gpt4 book ai didi

types - 你如何在 GraphQL 中扩展类型?

转载 作者:行者123 更新时间:2023-12-04 04:09:31 31 4
gpt4 key购买 nike

例如,PetAnimalownername .

type Animal {
species: String
}

type Pet extends Animal {
owner: Owner
name: String
}

最佳答案

June2018 stable version of the GraphQL spec 开始,一个 Object 类型可以扩展另一个 Object 类型:

Object type extensions are used to represent a type which has been extended from some original type. For example, this might be used to represent local data



在你的例子中,
type Animal {
species: String
}

extend type Animal {
owner: Owner
name: String
}

这本身不是继承;您只能扩展基本类型,而不能基于它创建新类型。请注意,新类型没有名称;现有 Animal类型被扩展。

graphql.org documentation没有提及有关 extend 的任何内容,但文档是 admittedly lackluster并且正在 transitioned从 Facebook 的所有权到 Linux 基金会。
JavaScript 引用实现 doesn't fully support extensions ,但是因为您已经标记了您的问题 ,您可以使用 graphql-tools , 其中 does :

const { graphql } = require('graphql');
const { makeExecutableSchema } = require('graphql-tools');

const typeDefs = `
type Person {
name: String!
}
extend type Person {
salary: Int
}
type Query {
person: Person
}
`;

const resolvers = {
Query: {
person: () => ({ name: "John Doe", salary: 1234 })
}
}

const schema = makeExecutableSchema({ typeDefs, resolvers });

graphql(schema, '{ person {name salary} }').then((response) => {
console.log(response);
});

对于实际的类型继承,请参见 graphql-s2s library .

关于types - 你如何在 GraphQL 中扩展类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48917863/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com