标签: graphql-js

GraphQL/中继架构“无法查询字段...”

我在查询 graphql/relay 中的某些字段时遇到问题。我正在尝试在下面的架构中查询 Courses 上的“courseName”字段。

如果我查询用户,例如:

{ 用户(id:1){名字,姓氏}}

它工作并返回正确的响应。

这是我的架构:

import {
  GraphQLBoolean,
  GraphQLFloat,
  GraphQLID,
  GraphQLInt,
  GraphQLList,
  GraphQLNonNull,
  GraphQLObjectType,
  GraphQLSchema,
  GraphQLString,
} from 'graphql';

import {
  connectionArgs,
  connectionDefinitions,
  connectionFromArray,
  fromGlobalId,
  globalIdField,
  mutationWithClientMutationId,
  nodeDefinitions,
} from 'graphql-relay';

import {
  // Import methods that your schema can use to interact with your database
  User,
  Course,
  getUser,
  getCourses
} from './database';

/**
 * We get the node interface and field from the Relay library.
 *
 * The first method defines the way we …
Run Code Online (Sandbox Code Playgroud)

javascript graphql graphql-js relayjs

2
推荐指数
1
解决办法
6522
查看次数

React Relay更新根查询作为变异胖查询的一部分

我正在尝试使用GraphQL和React Relay构建应用程序.

作为其中的一部分,我使用以下规范创建了一个根查询:

query {
  AllServices {
    edges {
      node {
        id
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个名为CreateGithubService的突变 - 效果很好.

以下是Relay变异的片段:

getFatQuery() {
      return Relay.QL`
        fragment on CreateGithubServicePayload {
          createdService
        }
      `
  }

  getConfigs() {
      return [{
          type: "REQUIRED_CHILDREN",
          children: [
              Relay.QL`
                  fragment on CreateGithubServicePayload {
                      createdService {
                          id
                      }
                  }
              `
          ]
      }]
  }
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是这不会导致依赖于信息更新的视图.

我的AllServices查询未被重新获取,我无法在Fat Query中指定它.

如何设置我的变异以将元素添加到所有服务查询?

谢谢!

reactjs graphql graphql-js relayjs

2
推荐指数
1
解决办法
825
查看次数

如何处理 GraphQL Schema 定义中的连字符

我的猫鼬模式如下

var ImageFormats = new Schema({
     svg        : String,
     png-xlarge : String,
     png-small  : String
});
Run Code Online (Sandbox Code Playgroud)

当我将其翻译成 GraphQL 模式时,这就是我尝试的

export var GQImageFormatsType: ObjectType = new ObjectType({
     name: 'ImageFormats',

     fields: {
          svg        : { type: GraphQLString },
         'png-xlarge': { type: GraphQLString },
         'png-small' : { type: GraphQLString }
 }
});
Run Code Online (Sandbox Code Playgroud)

GraphQL 返回以下错误: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

如果我尝试在 Mongoose 模型之后对 GraphQL 进行建模,我该如何协调这些字段?有没有办法让我创建别名?

(我在 graffiti 和 stackoverflow 论坛上搜索过这个,但找不到类似的问题)

mongoose mongodb mongoose-schema graphql graphql-js

2
推荐指数
1
解决办法
3413
查看次数

有嵌套突变的Graphql?

我试图找出如何使用graphql突变变异嵌套对象,如果可能的话.例如,我有以下架构:

type Event {
    id: String
    name: String
    description: String
    place: Place
}

type Place {
    id: String
    name: String
    location: Location
}

type Location {
    city: String
    country: String
    zip: String
}

type Query {
    events: [Event]
}

type Mutation {
    updateEvent(id: String, name: String, description: String): Event
}

schema {
    query: Query
    mutation: Mutation
}
Run Code Online (Sandbox Code Playgroud)

如何在我的updateEvent变异中添加地点信息?

graphql graphql-js

2
推荐指数
2
解决办法
1万
查看次数

如何使用GraphQL Relay连接修复循环依赖关系

我已将我与该文件的连接分离出来并可以在我的根查询中使用它(当在类型中定义连接时它工作正常),但是当尝试通过将其导入到类型时使用它,连接是为了我回来:

错误:PageEdge.node字段类型必须是输出类型,但得到:undefined

我可以在QueryType中使用连接,但不能使用PageType.我的架构大致是这样的.

QueryType {
  pages: [PageType]
}

PageType {
  _id: string
  pages: [PageType]
}
Run Code Online (Sandbox Code Playgroud)

这是一个项目仓库的链接,其中graphql的东西是(我只是推了一个提交,错误地看到所有的代码):https: //github.com/DaveyEdwards/myiworlds/tree/master/src/data

我已经使QueryType和PageType字段thunk(似乎解决了大多数人的问题的解决方案),并尝试从接口中创建一个thunk:

interfaces: () => [nodeInterface]
Run Code Online (Sandbox Code Playgroud)

对于任何与用户和朋友构建应用程序的人来说,我认为这是一个非常常见的问题.

UserType: {
  friends: [UserType]
}
Run Code Online (Sandbox Code Playgroud)

我的PageType:

import { nodeInterface } from '../nodeInterface';
import PageConnection from './connections/PageConnection';

const PageType = new ObjectType({
  name: 'Page',
  description: 'Everything you see can be placed inside a page.',
  fields: () => ({
    id: globalIdField('Page', page => page._id),
    _id: {
      type: new NonNull(ID),
      description: 'A unique id used to instantly locate …
Run Code Online (Sandbox Code Playgroud)

javascript relay graphql graphql-js relayjs

2
推荐指数
1
解决办法
791
查看次数

GraphQL-js 突变可选参数

我将如何在具有可选参数的 nodeJS 中实现 GraphQL Mutation?

我当前的突变有一个 args 字段,但所有参数都是强制性的。由于我在文档中找不到任何内容,因此我不知道这是否可行或是否可行。

这是我当前的代码:

const fakeDB = {
    count: 0
};

const schema = new GraphQLSchema({
    query: //...
    mutation: new GraphQLObjectType({
        name: 'adsF',
        fields: {
            updateCount: {
                type: GraphQLInt,
                args: {
                    count: { type: GraphQLInt } // I want to make this argument optional
                },
                resolve: (value, { count }) => {
                    // Catch if count is null or undefined
                    if (count == null) {
                        // If so just update with default
                        fakeDB.count = 5; …
Run Code Online (Sandbox Code Playgroud)

javascript node.js graphql-js

2
推荐指数
1
解决办法
9617
查看次数

如何设置GraphQL查询,以便需要一个或另一个参数,但不能同时使用这两个参数

我刚刚掌握了GraphQL,

我已经设置了以下查询:

type: UserType,
args: {
    id:    { name: 'id',    type: new GraphQLNonNull(GraphQLID)     },
    email: { name: 'email', type: new GraphQLNonNull(GraphQLString) }
},
resolve: (root, { id, email }, { db: { User } }, fieldASTs) => {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我希望能够将'id'或'email'传递给查询,但是,通过此设置,它需要传递id和电子邮件.

有没有办法设置查询所以只需要一个参数,无论是id还是电子邮件,但不是两者都有?

graphql graphql-js

2
推荐指数
1
解决办法
871
查看次数

如何为嵌套对象或同一父对象中的对象列表定义GraphQLObjectType

对于我的MongoDB对象,我有一个架构,其中有许多嵌套数据。

但是我在GraphQL中完美实现了查询以获取数据,但是由于无法定义类型,因此无法获取嵌套数据。

fields() {
        return {
            name: {
                type: GraphQLString,
                description: 'Name of the person'
            },
            age: {
                type: GraphQLString,
                description: 'Age'
            },
            documents: { // what to do here
                type: new GraphQLList(),
                description: 'All documents for the person'
            }
       }
}
Run Code Online (Sandbox Code Playgroud)

原始数据是这样的。

{
    "_id" : ObjectId("5ae1da5e4f00b5eee4ab84ee"),
    "name" : "Indira Gandhi International Airport",
    "age" : 54,
    "documents" : [
        {
             "doc_name" : "personal card",
             "doc_url" : "http://",
             "status" : true
        },
        {
             "doc_name" : "bank card",
             "doc_url" : "http://",
             "status" …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js graphql graphql-js

2
推荐指数
1
解决办法
451
查看次数

如何用express-graphql引发多个错误?

在express-graphql应用程序中,我有一个userLogin解析器,如下所示:

const userLogin = async ({ id, password }), context, info) => {

    if (!id) {
      throw new Error('No id provided.')
    }

    if (!password) {
      throw new Error('No password provided.')
    }

    // actual resolver logic here
    // … 
}
Run Code Online (Sandbox Code Playgroud)

如果用户不提供idAND AND password,则只会抛出一个错误。

{
  "errors": [
    {
      "message": "No id provided.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "userLogin"
      ]
    }
  ],
  "data": {
    "userLogin": null
  }
}
Run Code Online (Sandbox Code Playgroud)

如何在errors响应数组中引发多个错误?

javascript graphql graphql-js express-graphql

2
推荐指数
1
解决办法
591
查看次数

是否可以将变量放在GraphQL标签内?

现在我在下面有这个标签。它是静态的,将始终获得ID为3的注释。是否有可能在此graphQL-tag中放入变量。因此,我可以重新使用graphQL-tag,仅更改变量ID?

export const GET_COMMENTS: any = gql`
    {
        comments(id: 3) {
            userId,
            text,
            creationDate,
      }
    }
`;
Run Code Online (Sandbox Code Playgroud)

提前致谢!

graphql graphql-js graphql-tag

2
推荐指数
1
解决办法
2058
查看次数