I am sorry if this is somewhere documented but I couldn't find any resources about this.
Is it possible to specify a root mutation like rootValue does for the query?
My goal is to use buildSchema to build my schema and then have a resolver for the mutation and query.
Something like this:
import { graphql, buildSchema } from 'graphql'
const users = []
const schema = buildSchema(`
type User {
username: String
password: String
}
type Query {
users: [User]
}
type Mutation {
signup (username: String, password: String): User
}
`)
const query = {
user: () => users
}
const mutation = {
signup: (user) => users.push(user)
}
graphql(schema, '{ users { username } }', query, /* mutation? */).then((response) => {
console.log(response);
});
Hi @k15a - you might have forgotten to define a schema definition documented here:
schema {
query: Query
mutation: Mutation
}
@asiandrummer i dont remember when exactly, but this is not a requirement anymore.
if not defined, Query & Mutation will be used.
@k15a , why using root object for this instead of context?
context is much more suitable for this.
@DxCx How should context help with this problem? Isn't context something to pass for example the auth code or a db connection down?
for example, this should work:
import { graphql, buildSchema } from 'graphql'
const users = []
const schema = buildSchema(`
type User {
username: String
password: String
}
type Query {
users: [User]
}
type Mutation {
signup (username: String, password: String): User
}
`)
const context = {
users: users
}
const mutation = {
signup: (root, args, ctx) => ctx.users.push({ username: args.username });
}
graphql(schema, '{ users { username } }', context).then((response) => {
console.log(response);
});
context is the object used to connect the schema to any outer layer.
in your case, thats an array of users that should be filled.
it is also used, as you mentioned, for communicating with databases..
i just noticed that you didn't connect resolvers to the schema, so mutation isn't really going to work,
but you should be able to get the idea...
Yeah that's my problem :D how can I connect the mutation to the schema?
http://dev.apollodata.com/tools/graphql-tools/generate-schema.html
Enjoy :)
So it's not supported by graphql-js?
I'm not sure how it should be done with bare graphql-js,
maybe someone else can shade some lights..
Just curious the anwser and find orginal PR https://github.com/graphql/graphql-js/pull/471 . Maybe could get some help from @lacker?
The same rootValue is used for all types of operations, so to build on your original example @k15a, you could write:
const rootValue = {
user: () => users,
signup: (user) => users.push(user)
}
graphql(schema, '{ users { username } }', rootValue).then((response) => {
console.log(response);
});
Or if you're using Babel and have object spread syntax:
const query = {
user: () => users
}
const mutation = {
signup: (user) => users.push(user)
}
graphql(schema, '{ users { username } }', {...query, ...mutation}).then((response) => {
console.log(response);
});
Most helpful comment
The same
rootValueis used for all types of operations, so to build on your original example @k15a, you could write:Or if you're using Babel and have object spread syntax: