This schema is valid and works.
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: userType,
args: {
id: { type: GraphQLString }
},
resolve: (source, args, context, info) => {
console.log(source, args, context, info)
return Promise.resolve({"name": 'meow'})
}
}
}
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
createUser: {
type: userType,
args: {
name: {
name: 'name',
type: GraphQLString
},
id: {
name: 'id',
type: GraphQLInt
}
},
resolve: (source, args, context, info) => {
console.log(source, args, context, info)
return Promise.resolve({"name": 'meow'})
}
}
}
})
})
When I remove the query schema like this:
var schema = new GraphQLSchema({
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
createUser: {
type: userType,
args: {
name: {
name: 'name',
type: GraphQLString
},
id: {
name: 'id',
type: GraphQLInt
}
},
resolve: (source, args, context, info) => {
console.log(source, args, context, info)
return Promise.resolve({"name": 'meow'})
}
}
}
})
})
I get this error:
Error: Schema query must be Object Type but got: undefined.
Seems like It should be valid for me to create a schema without a query and only mutations. Thoughts?
It's invalid to create a schema without a query type. http://facebook.github.io/graphql/#sec-Initial-types
Do you have a use case for a write-only GraphQL service? I'm curious.
Closing, but feel free to continue discussion.
A microservice with a GraphQL API that simply sends emails would not necessarily need any queries.
I wouldn't necessarily recommend GraphQL for a microservice like that. You might be better off with something much simpler for such a use case.
Would there be a "complexity lower bound" for best practice GraphQL use? Possibly: "at least one query and mutation".
At least one query :)
GraphQL's strength is in allowing you to request data in a specific shape and evolve an API over time without breaking existing queries.
Mutations in GraphQL are valuable, but primarily because they offer an API of write-then-read which lets you follow a mutation with a specific query in the same atomic request.
For services that are write-only or have fire-and-forget mutations, you're really not leveraging GraphQL's strengths and so you're "paying" for an abstraction that you're not gaining from.
I'm building a graphql server that doesn't have any queries. Mainly proxy to and from other apis. I'm just setting a simple author query which is nice to have to just test and see if there's connection to the graphql and perform the minimal query. While I'm not a big proponent of needing to have a query. It's not a big issue for me. Just have a project, name, alive, health, or author query that returns a string or boolean.
The author query helps to see if CORS works correctly on our server.
{
query: new GraphQLObjectType({
name: 'Query',
fields: {
author: {
type: GraphQLString,
resolve: (source, args, context, info) => {
return 'Thomas Reggi'
}
}
}
})
}
fetch('http://localhost:3000/graphql', {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"query": "{\n author\n}",
"variables": null,
"operationName": null
})
}).then(function (res){
res.json().then(console.log)
}).catch(console.error)
I have some use cases for a server that only has subscriptions and no queries.
i have the same err with a schema which has only queries and no other types, this should be valid though should it not?
Most helpful comment
GraphQL's strength is in allowing you to request data in a specific shape and evolve an API over time without breaking existing queries.
Mutations in GraphQL are valuable, but primarily because they offer an API of write-then-read which lets you follow a mutation with a specific query in the same atomic request.
For services that are write-only or have fire-and-forget mutations, you're really not leveraging GraphQL's strengths and so you're "paying" for an abstraction that you're not gaining from.