【问题标题】:GraphQL : the object name is defined in resolvers, but not in schemaGraphQL:对象名称在解析器中定义,但不在模式中
【发布时间】:2018-10-04 01:13:06
【问题描述】:

我想使用 graphql 定义一个突变。

我的变异是获取一个对象作为参数。所以我在模式和解析器中使用 GraphQLObjectType 定义了新对象。

但是我得到了这个错误:

错误:Agreement.name 在解析器中定义,但不在架构中

有什么想法吗?

这是我的架构定义

const typeDefs = `

    type Agreement {
        id: Int
    }

    type Mutation {
        agreementsPost(agreement: Agreement) : String
    }
`;

这是我的解析器:

const appResolvers = {

    Agreement: new GraphQLObjectType({
        name: 'Agreement',
        fields: {
            id: { type: GraphQLInt },
        }
    }),
Mutation: {

       agreementsPost(root, args) {
            return axios.post("....").then(res => res.data);
        },
    }

【问题讨论】:

    标签: graphql apollo


    【解决方案1】:

    额外数据(与错误有关 - 与 Q 码无关)。

    你好世界

    我们在 ma​​p 中定义解析器,其中映射的 键对应到我们的架构类型。 https://www.apollographql.com/docs/tutorial/resolvers/

    这个“错误地图”错误的最基本“hello world”示例

    目的我错了(在解析器定义下 - 使用 hello2 而不是 hello)。

    graphql-yoga 服务器示例:

    /*index.js*/
    const { GraphQLServer } = require('graphql-yoga')
    
    const typeDefs = `
      type Query {
        hello(name: String): String!
      }
    `
    
    const resolvers = {
      Query: {
        hello2: (_, { name }) => `Hello ${name || 'World'}`,
      },
    }
    
    const server = new GraphQLServer({ typeDefs, resolvers })
    server.start(() => console.log('Server is running on localhost:4000'))
    

    抛出错误:

    [错误:Query.hello2 在解析器中定义,但不在架构中]

    解析器更改为hello(匹配hello架构类型)以修复此错误:

    相关:

    【讨论】:

      【解决方案2】:

      我在从 apollo-server-express v1 迁移到 v2 时遇到此错误,该错误与未在架构中定义上传有关。现在,在声明您的 graphql 架构时,您可以使用选项集 uploads: false

      // GraphQL: Schema
      const SERVER = new ApolloServer({
        typeDefs: typeDefs,
        resolvers: resolvers,
        introspection: true,
        uploads: false,
        playground: {
          endpoint: `http://localhost:3000/graphql`,
          settings: {
            'editor.theme': 'light'
          }
        }
      });
      

      如果您的错误是 Uploads 特定的,则似乎可以解决这种情况下的问题

      【讨论】:

        【解决方案3】:

        在我的情况下,这是因为 非 null 的架构不一致。在我的突变中,我没有非空突变,而在对象模式中它有非空突变。

        【讨论】:

          【解决方案4】:

          这里有几件事要解决。首先,要将对象用作参数,您必须在架构中将其定义为input(或GraphQLInputObjectType)——您不能使用常规的type(或GraphQLObjectType)作为参数。

          所以你的类型定义需要看起来像这样:

          type Mutation {
            agreementsPost(agreement: Agreement): String
          }
          
          input Agreement {
            id: Int
          }
          

          如果您已有 Agreement 类型,则需要将输入命名为其他名称。将Input 附加到您的类型名称是一个很好的约定:

          type Mutation {
            agreementsPost(agreement: AgreementInput): String
          }
          
          type Agreement {
            id: Int
          }
          
          input AgreementInput {
            id: Int
          }
          

          这应该足以让您将AgreementInput 对象作为参数传递给您的突变。您无需将 AgreementAgreementInput 添加到解析器(事实上,GraphQL 不会“解析”输入,因此无法为输入添加解析器)。

          也就是说,您的解析器对象不需要包含graphql 包提供的任何类型构造函数——当您调用makeExecutableSchema 时,Apollo 从您的解析器和类型定义中构造一个GraphQLSchema 对象。

          如果您的类型定义包括FooBar 类型,您的resolvers 对象可能如下所示:

          const resolvers = {
            Foo: {
              someFooProperty: (foo, args, context, info) => {}
            },
            Bar: {
              someBarProperty: (bar, args, context, info) => {}
              someOtherBarProperty: (bar, args, context, info) => {}
            },
            Query: {
              someQuery: (root, args, context, info) => {}
            },
            Mutation: {
              someMutation: (root, args, context, info) => {}
            },
          }
          

          注意resolvers 对象中的每个属性如何匹配您的架构中定义的类型之一(包括查询和突变)。每个属性的值本身就是一个对象,每个属性映射到为该特定类型定义的字段之一。每个字段的值都是您的resolve 函数。

          您看到错误的原因是您已有效地告诉 makeExecutableSchema 将解析器添加到协议类型上的两个字段 - namefields - 这两个字段实际上都不在您的根据您的类型定义的架构。

          您可以阅读有关如何使用 Apollo here 生成架构的更多信息。您可能会看到仅使用 GraphQL.js 通过定义 GraphQLSchema 对象并将其传递给中间件来“以编程方式”生成模式的示例。这两种方法各有利弊,但使用makeExecutableSchema 通常更容易且不易出错。无论哪种方式,了解如何以编程方式生成模式都很好,但您不应该将两者混为一谈!

          【讨论】:

          • 太棒了!它有效,非常感谢您的完美回复
          猜你喜欢
          • 2019-08-27
          • 2018-10-23
          • 2020-07-21
          • 2019-02-08
          • 2019-09-06
          • 2017-02-14
          • 2020-02-21
          • 2019-10-01
          • 2019-12-21
          相关资源
          最近更新 更多