【问题标题】:Unknown argument in GraphiQL trying to pass an ID from the client to the GraphQL server?GraphiQL 中的未知参数试图将 ID 从客户端传递到 GraphQL 服务器?
【发布时间】:2018-07-02 09:44:05
【问题描述】:

我正在使用 Apollo 2。通过 GraphiQL 界面,我可以运行一个返回所有组的查询。但是,当我尝试传递名称以仅找到 1 个组时,它不起作用。

架构

type Query {
    hi: String
    groups: [Group]
    group(name: String): Group
}

type Group {
    name: String
}

解析器:

Query: {
        hi() {
            return 'howdy';
        },
        groups() {
            return Groups.find().fetch();
        },
        group(one, two, three) {
            console.log('group resolver called');
            console.log(one, two, three);
            //return Groups.findOne(two.name);
        },
    },

这是我的 GraphiQL 组查询,工作正常:

{
  groups {
    name
  }
}

但是我的组查询返回错误:

{
  group(name: "some name") {
    name
  }
}

{
  "errors": [
    {
      "message": "Unknown argument \"name\" on field \"group\" of type \"Query\".",
      "locations": [
        {
          "line": 2,
          "column": 9
        }
      ]
    }
  ]
}

更新 - 这是我的完整文件:

import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import merge from 'lodash/merge';

import GroupsSchema from '../../api/groups/Groups.graphql';
import GroupsResolvers from '../../api/groups/resolvers';

const typeDefs = [GroupsSchema];
const resolvers = merge(GroupsResolvers);

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

createApolloServer({ schema });

在 resolvers.js 中

import Groups from './groups';

export default {
    Query: {
        hi() {
            return 'howdy';
        },
        groups() {
            return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
            // return Groups.find().fetch();
        },
        group(one, two, three) {
            console.log('group resolver called');
            console.log(one, two, three);
            // return Groups.findOne('Apon9HKE9MeZqeXsZ');
        },
    },
};

在 Groups.graphql 中

type Query {
    hi: String
    groups: [Group]
    group(name: String): Group
}

type Group {
    name: String
}

【问题讨论】:

  • 您是否尝试在解析器中将“组(一、二、三)”更改为“组(名称)”?
  • 我试过 group(root, { name }) 和 group(name)。我收到完全相同的错误消息。
  • 您可以添加文件的所有内容吗?解析器和架构?
  • @AlessanderFrança 完成。
  • 我认为应该是 group(root, { name }),但你告诉过它不起作用。为什么要在解析器中导入组架构?

标签: graphql apollo-client


【解决方案1】:

我认为你的 typeDefs 和解析器是错误的,试试:

类型定义

const typeDefs = `
  type Query {
    hi: String
    groups: [Group]
    group(name: String): Group
}

type Group {
    name: String
}`;

解析器

export default {
        Query: {
            hi() {
                return 'howdy';
            },
            groups() {
                return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
                // return Groups.find().fetch();
            },
            group(_, { name }) {
                console.log('group resolver called');
                console.log(one, two, three);
                // return Groups.findOne('Apon9HKE9MeZqeXsZ');
            },
        },
    };

【讨论】:

    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多