graphql-go / graphql-go/graphql

Schema not working with only Mutation field

Open
#414 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

I am getting the same problem of the [#issue187](https://github.com/graphql-go/graphql/issues/187), but with a different behaviour. @chris-ramon
I want to create a schema which has **only mutation field**. It doesn't need query field.
```go
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Mutation: rootMutation,
})
```
In this way, **graphiql** browser gets the following error from graphql:
```json
{
"data": null,
"errors": [
{
"message": "Unknown type \"__Type\".",
"locations": [
{
"line": 20,
"column": 24
}
]
},
{
"message": "Unknown type \"__InputValue\".",
"locations": [
{
"line": 53,
"column": 26
}
]
},
{
"message": "Unknown type \"__Type\".",
"locations": [
{
"line": 60,
"column": 23
}
]
}
]
}
```
So, documentation loads nothing (no schemas).
That is a problem related to query field. Indeed, if I change the code, browser shows the documentation (under query navigation and not mutation of course).
```go
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootMutation,
})
```

I am reading the code at `graphql/schema.go` and this is pretty strange to me. Why query must be checked before mutation exiting with an error if is nil? I don't understand.

```go
func NewSchema(config SchemaConfig) (Schema, error) {
var err error

schema := Schema{}

if err = invariant(config.Query != nil, "Schema query must be Object Type but got: nil."); err != nil {
return schema, err
}

// if schema config contains error at creation time, return those errors
if config.Query != nil && config.Query.err != nil {
return schema, config.Query.err
}
if config.Mutation != nil && config.Mutation.err != nil {
return schema, config.Mutation.err
}
...
```

_Originally posted by @limoli in https://github.com/graphql-go/graphql/issues/187#issuecomment-431326890_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.