简体   繁体   English

GraphQL / Relay架构无法在“CreateLinkPayload”类型上查询字段“store”

[英]GraphQL/Relay Schema Cannot query field “store” on type “CreateLinkPayload”

I can successfully do graphql/relay queries and mutations with CURL and GraphiQL tool: 我可以使用CURL和GraphiQL工具成功完成graphql / relay查询和突变:

在此输入图像描述

However, in my react/relay app I can query and get the data into the app, but every time I try to mutate something in my app, I get this error in the console: 但是,在我的react / relay应用程序中,我可以查询并将数据导入应用程序,但每次我尝试在应用程序中改变某些内容时,我都会在控制台中收到此错误:

    bundle.js:51511 Uncaught Error: GraphQL validation error ``Cannot query field "store" on type "CreateLinkPayload".`` in file 
`/Users/johndoe/react-relay-project/src/mutations/CreateLinkMutation.js`. Try updating your GraphQL schema if an argument/field/type was recently added.
(anonymous function) @ bundle.js:51511
getFatQuery @ bundle.js:51512
getFatQuery @ bundle.js:35664
getQuery @ bundle.js:35791
_handleCommit @ bundle.js:35539
commit @ bundle.js:35453
commit @ bundle.js:35894
(anonymous function) @ bundle.js:28526

and every time I do npm start I get this error: 每次我做npm start我都会收到此错误:

-- GraphQL Validation Error -- CreateLinkMutation --

File:  /Users/johndoe/react-relay-project/src/mutations/CreateLinkMutation.js
Error: Cannot query field "store" on type "CreateLinkPayload".
Source:
> 
>         store { linkConnection }
>         ^^^

CreateLinkMutation.js

import Relay from 'react-relay'

class CreateLinkMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`
      mutation { createLink }
    `
  }

  getVariables() {
    return {
      title: this.props.title,
      url: this.props.url
    }
  }

  getFatQuery() {
    return Relay.QL`
      fragment on CreateLinkPayload {
        linkEdge,
        store { linkConnection }
      }
    `
  }

  getConfigs() {
    return [{
      type: 'RANGE_ADD',
      parentName: 'store',
      parentID: this.props.store.id,
      connectionName: 'linkConnection',
      edgeName: 'linkEdge',
      rangeBehaviors: {
        '': 'append'
      }
    }]

  }
}

export default CreateLinkMutation

parts of Link.js Link.js

Link = Relay.createContainer(Link, {
  fragments: {
    link: () => Relay.QL`
      fragment on Link {
        url,
        title
      }
    `
  }
})

parts of App.js App.js

 handleSubmit(e) {
    e.preventDefault()
    Relay.Store.update(
      new CreateLinkMutation({
        title: this.refs.newTitle.value,
        url: this.refs.newUrl.value,
        store: this.props.store
      })
    )
    this.refs.newTitle.value = ''
    this.refs.newUrl.value = ''
  }



App = Relay.createContainer(App, {
  initialVariables: {
    limit: 10
  },
  fragments: {
    store: () => Relay.QL`
      fragment on Store {
        id,
        linkConnection(first: $limit) {
          edges {
            node {
              id,
              ${Link.getFragment('link')}
            }
          }
        }
      }
    `

  }
})

parts of main.js main.js

class LinkStoreRoute extends Relay.Route {
  static routeName = 'LinkStoreRoute'
  static queries = {
    store: () => Relay.QL`query { store }`
  }
}

My schema.js : 我的schema.js

const store = {}

const Store = new GraphQLObjectType({
  name: 'Store',
  fields: () => ({
    id: globalIdField('Store'),
    linkConnection: {
      type: linkConnection.connectionType,
      args: connectionArgs,
      resolve: (_, args) => {
        return docClient.scan(
          Object.assign(
            {},
            {TableName: linksTable},
            paginationToParams(args)
          )
        ).promise().then(dataToConnection)
      }
    }
  })
})

const Link = new GraphQLObjectType({
  name: 'Link',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      resolve: (obj) => obj.id
    },
    title: { type: GraphQLString },
    url: { type: GraphQLString }
  })
})

const linkConnection = connectionDefinitions({
  name: 'Link',
  nodeType: Link
})

let createLinkMutation = mutationWithClientMutationId({

  name: 'CreateLink',

  inputFields: {
    title: { type: new GraphQLNonNull(GraphQLString) },
    url: { type: new GraphQLNonNull(GraphQLString) }
  },

  outputFields: {
    linkEdge: {
      type: linkConnection.edgeType,
      resolve: (obj) => ({node: obj, cursor: obj.id})
    }
  },

  store: {
    type: Store,
    resolve: () => store
  },

  mutateAndGetPayload: (inputFields) => {

    let link = {
      id: uuid.v4(),
      title: inputFields.title,
      url: inputFields.url
    }

    return new Promise((resolve, reject) => {
      docClient.put(
        Object.assign(
          {},
          {TableName: linksTable},
          {Item: link}
        ),
        (err, data) => {
          if (err) return reject(err)
          return resolve(link)
        }
      )
    })
  }
})

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      store: {
        type: Store,
        resolve: () => store
      }
    })
  }),

  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: () => ({
      createLink: createLinkMutation
    })
  })
})

module.exports = schema

Note that I excluded 40 lines of require statements at the top. 请注意,我在顶部排除了40行require语句。

How is this even possible? 这怎么可能呢? And how can I fix it? 我该如何解决? Could it be a bug of relay/react? 这可能是继电器/反应的错误吗?

In your client-side implementation of CreateLinkMutation (CreateLinkMutation.js file), getFatQuery() function specifies that it expects two outputs after the mutation is complete - linkEdge and store . CreateLinkMutation (CreateLinkMutation.js文件)的客户端实现中, getFatQuery()函数指定在变异完成后它需要两个输出 - linkEdgestore Therefore, in your server-side implementation of the mutation, you must include these two things as output. 因此,在服务器端实现变异时,必须将这两项内容作为输出。 However, only linkEdge is included to the mutation's outputs in your current implementation ( createLinkMutation in schema.js file). 然而,只有linkEdge包括到突变在当前的实现(输出createLinkMutation在schema.js文件)。 To solve the problem, include store to the outputs. 要解决此问题, store包含在输出中。

outputFields: {
  linkEdge: {
    type: linkConnection.edgeType,
    resolve: (obj) => ({node: obj, cursor: obj.id})
  },
  store: {
    type: Store,
    resolve: () => store
  },
},

Turns out store 结果商店

  store: {
    type: Store,
    resolve: () => store
  }

has to go inside outputFields like so: 必须像这样进入outputFields:

  outputFields: {
    linkEdge: {
      type: linkConnection.edgeType,
      resolve: (obj) => ({node: obj, cursor: obj.id})
    }
    store: {
      type: Store,
      resolve: () => store
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQL /中继架构“无法查询字段...” - GraphQL/Relay Schema “Cannot query field…” 如何在 GraphQL (Relay) 中查询和改变数组类型? - How to query and mutate array type in GraphQL (Relay)? 收到错误:类型为“查询” [GraphQL,Relay,React]的字段“用户”上的未知参数“ id” - Getting error : Unknown argument “id” on field “user” of type “Query” [GraphQL, Relay, React] 您的 GraphQL 查询中出现错误:无法在“StrapiPropiedadesImagen”类型上查询字段“childImageSharp” - There was an error in your GraphQL query: Cannot query field “childImageSharp” on type “StrapiPropiedadesImagen” 在添加新架构字段后,aws cdk nextjs graphql 突变无法为不可空类型返回 null - aws cdk nextjs graphql mutation cannot return null for non-nullable type after adding in new schema field IMDB API 请求 GraphQL 错误 'ClientError: 无法查询类型为“MainSearchEntity”的字段“图像”' - IMDB API request GraphQL Error 'ClientError: Cannot query field "Image" on type "MainSearchEntity"' 如何在中继和graphql模式中正确创建连接? - How do I properly create a connection in relay & graphql schema? 无法为 GraphQL 查询中的不可空字段返回 null - Cannot return null for non-nullable field in a GraphQL query relay / graphql:可为空的响应或捕获查询错误的方法 - relay/ graphql: nullable response or a way to catch query error NestJs - Graphql 错误无法确定 GraphQL 输入类型<field> . 确保您的 class 已使用合适的装饰器进行装饰</field> - NestJs - Graphql Error Cannot determine a GraphQL input type for the <Field>. Make sure your class is decorated with an appropriate decorator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM