gographqlschemagraphql-go

Graphql : Schema is not configured for mutations in Go


I am trying to write Graphql Mutation inside Go. I am using github.com/graphql-go/graphql library.

Here is the code i have written for the same.

package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"

  "github.com/graphql-go/graphql"
)

type userMaster struct {
  Name string `json:"userName"`
  ID   string `json:"emailID"`
}

var user userMaster

func main() {
fmt.Println("Hello world !!!")

userType := graphql.NewObject(
    graphql.ObjectConfig{
        Name: "userMaster",
        Fields: graphql.Fields{
            "userName": &graphql.Field{
                Type: graphql.String,
            },
            "emailID": &graphql.Field{
                Type: graphql.String,
            },
        },
    },
)

rootMutation := graphql.NewObject(graphql.ObjectConfig{
    Name: "Mutations",
    Fields: graphql.Fields{
        "createUser": &graphql.Field{
            Type: userType,
            Args: graphql.FieldConfigArgument{
                "userName": &graphql.ArgumentConfig{
                    Type: graphql.NewNonNull(graphql.String),
                },
                "emailID": &graphql.ArgumentConfig{
                    Type: graphql.NewNonNull(graphql.String),
                },
            },
            Resolve: func(params graphql.ResolveParams) (interface{}, error) {
                user.Name = params.Args["userName"].(string)
                user.ID = params.Args["emailID"].(string)
                return user, nil
            },
        },
    },
})

schema, _ := graphql.NewSchema(graphql.SchemaConfig{
    Mutation: rootMutation,
})

http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
    result := graphql.Do(graphql.Params{
        Schema:        schema,
        RequestString: r.URL.Query().Get("query"),
    })
    json.NewEncoder(w).Encode(result)
})

http.ListenAndServe(":8080", nil)
}

When i am trying to run command from postman I am getting error message {"data":null,"errors":[{"message":"Schema is not configured for mutations","locations":[{"line":1,"column":1}]}]}

http://localhost:8080/graphql?query=mutation+_{createUser(userName:"ABC",emailID:"abc@abc.com"){userName,emailID}}

this is the URL I am trying to hit from postman. I am referring Getting Started With GraphQL Using Golang to implement mutation in Go

Can anyone please help what changes needs to be done here?


Solution

  • The shema you are using for the mutation requires a query object.

    schema, _ := graphql.NewSchema(graphql.SchemaConfig{
        // QUERY NEEDED HERE !
        Mutation: rootMutation,
    })
    

    A query for userType can be defined like this and added to the schema.

    rootQuery := graphql.NewObject(graphql.ObjectConfig{
        Name: "Query",
        Fields: graphql.Fields{
            "lastUser" : &graphql.Field{ // "lastUser" name can be anything
                Type: userType,
            },
        },
    })
    
    schema, _ := graphql.NewSchema(graphql.SchemaConfig{
        Query:    rootQuery,
        Mutation: rootMutation,
    })
    

    This will fix the "Schema is not configured for mutations" error and have your mutations successfully performed.