Evanss
Evanss

Reputation: 23593

Unknown argument in GraphiQL trying to pass an ID from the client to the GraphQL server?

Im using Apollo 2. With the GraphiQL interface I can run a query which returns all groups fine. However when I try to pass a name to find just 1 group it doenst work.

Schema

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

type Group {
    name: String
}

Resolvers:

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);
        },
    },

This is my GraphiQL groups query which works fine:

{
  groups {
    name
  }
}

But my group query returns an error:

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

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

UPDATE - This is my full file:

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 });

In 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');
        },
    },
};

In Groups.graphql

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

type Group {
    name: String
}

Upvotes: 0

Views: 1357

Answers (1)

Alessander Franca
Alessander Franca

Reputation: 2753

I think your typeDefs and resolvers are wrongs, try:

typeDefs

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

type Group {
    name: String
}`;

Resolvers

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');
            },
        },
    };

Upvotes: 1

Related Questions