Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  22] [ 7]  / answers: 1 / hits: 71350  / 7 Years ago, fri, september 8, 2017, 12:00:00

Hi I am trying to learn GraphQL language. I have below snippet of code.



// Welcome to Launchpad!
// Log in to edit and save pads, run queries in GraphiQL on the right.
// Click Download above to get a zip with a standalone Node.js server.
// See docs and examples at https://github.com/apollographql/awesome-launchpad

// graphql-tools combines a schema string with resolvers.
import { makeExecutableSchema } from 'graphql-tools';

// Construct a schema, using GraphQL schema language
const typeDefs = `
type User {
name: String!
age: Int!
}

type Query {
me: User
}
`;

const user = { name: 'Williams', age: 26};

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
me: (root, args, context) => {
return user;
},
},
};

// Required: Export the GraphQL.js schema object as schema
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
});

// Optional: Export a function to get context from the request. It accepts two
// parameters - headers (lowercased http headers) and secrets (secrets defined
// in secrets section). It must return an object (or a promise resolving to it).
export function context(headers, secrets) {
return {
headers,
secrets,
};
};

// Optional: Export a root value to be passed during execution
// export const rootValue = {};

// Optional: Export a root function, that returns root to be passed
// during execution, accepting headers and secrets. It can return a
// promise. rootFunction takes precedence over rootValue.
// export function rootFunction(headers, secrets) {
// return {
// headers,
// secrets,
// };
// };


Request:



{
me
}


Response:



{
errors: [
{
message: Field me of type User must have a selection of subfields. Did you mean me { ... }?,
locations: [
{
line: 4,
column: 3
}
]
}
]
}


Does anyone know what I am doing wrong ? How to fix it ?


More From » graphql

 Answers
41

From the docs:




A GraphQL object type has a name and fields, but at some point those
fields have to resolve to some concrete data. That's where the scalar
types come in: they represent the leaves of the query.




GraphQL requires that you construct your queries in a way that only returns concrete data. Each field has to ultimately resolve to one or more scalars (or enums). That means you cannot just request a field that resolves to a type without also indicating which fields of that type you want to get back.



That's what the error message you received is telling you -- you requested a User type, but you didn't tell GraphQL at least one field to get back from that type.



To fix it, just change your request to include name like this:



{
me {
name
}
}


... or age. Or both. You cannot, however, request a specific type and expect GraphQL to provide all the fields for it -- you will always have to provide a selection (one or more) of fields for that type.


[#56539] Tuesday, September 5, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;