graphql-go icon indicating copy to clipboard operation
graphql-go copied to clipboard

Feature/prefix roots

Open maoueh opened this issue 4 years ago • 0 comments

This enables Query, Mutation and Subscription to use the same name. This is backward compatible and activated through an option when parsing the schema.

By specifying graphql.PrefixRootFunctions() option (name up to discussion), a single RootResolver object can be used that implements three time the same name for different kind of operation (Query, Mutation, Subscription).

This was extracted from #317 and it's a variant implementation of #145 (I don't think we realized such work already existed prior doing our own fork, sorry for stepping on other toes).

Example

schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
}

type Subscription { hello: String! }
type Query { hello: String! }
type Mutation { hello: String! }

And the resolver and schema instantiation:

schema := graphql.MustParseSchema(document, &RootResolver{}, graphql.PrefixRootFunctions())

type RootResolver struct{}

func (r *RootResolver) QueryHello() string {
	return "Hello query!"
}

func (r *RootResolver) MutationHello() string {
	return "Hello mutation!"
}

func (sr *RootResolver) SubscriptionHello(context.Context) (chan string, error) {
	c := make(chan string)
	go func() {
		c <- "Hello subscription!"
		close(c)
	}()

	return c, nil
}

maoueh avatar Nov 11 '20 05:11 maoueh