Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose hooks to enable extending the GraphQL schema #300

Closed
cellis opened this issue Jan 6, 2017 · 10 comments
Closed

Expose hooks to enable extending the GraphQL schema #300

cellis opened this issue Jan 6, 2017 · 10 comments

Comments

@cellis
Copy link

cellis commented Jan 6, 2017

Let's say I have a query and I need to call out to some api (either internal or external) to fulfill it? What's the best way to accomplish this? Does postgraphql expose resolve hooks? For some things, obviously I can precompute the data by saving it to postgres, but i can forsee problems where it wouldn't work.

@benjie
Copy link
Member

benjie commented Jan 6, 2017

We don't currently support doing that through postgraphql, though if you understand the caveats you could try doing it through postgresql directly using foreign data wrappers. Definitely something that we have in mind through.

@idibidiart
Copy link

Very new to GraphQL so this may not make sense but I assume in all these types of SQL-to-GraphQL-Schema solutions that we get an actual GraphL schema generated statically and as such we should be able to extend that schema with new types and resolvers etc. Or does postgraphql implement its own graphql server that consumes the database schema directly and will ignore or throw in case the graphql schema does not map 1:1 to the db schema?

@danvk
Copy link

danvk commented Feb 27, 2017

It would be great to have some hooks which make this possible. It's the difference between postgraphql being an all-or-nothing solution and one which can fit into a larger system.

@zamiang and my use case: we have a database which contains our own data as well as user IDs from an external service which exposes a REST API. We'd like to expose a unified GraphQL API over both services. To fill in data from the external REST API, we'd need to issue a second request after the Postgres result comes back.

I imagine defining the GraphQL schema for something like this would be tricky. @benjie any thoughts on a good workaround for this now? We're thinking about having two independent GraphQL endpoints: one for our own data with PostGraphQL and another which wraps the external REST API. Not ideal and not very GraphQLy, but it will let us keep moving.

@benjie
Copy link
Member

benjie commented Feb 27, 2017

I'm afraid I can't give any solid advice on that; I've just added a couple tiny REST APIs where necessary for my requirements. You might try reading up on merging GraphQL APIs (I've no idea if this will be successful or not):

https://www.google.co.uk/search?q=combine+graphql+schemas&oq=combine+graphql+schemas

https://gist.github.com/icebob/553c1f9f1a9478d828bcb7a08d06790a

https://gist.github.com/voodooattack/b0484625a90191a4dde4aabae8884243

@cellis
Copy link
Author

cellis commented Feb 27, 2017 via email

@calebmer
Copy link
Collaborator

calebmer commented Mar 5, 2017

So we do have hooks internally to extend the schema. It’s actually how we implement procedures 😊. See: https://github.com/calebmer/postgraphql/blob/61fe4550751aa45c07783a6ae17fdcd01b73f956/src/postgraphql/schema/createPostGraphQLSchema.ts#L124-L139

I think a very good first step towards this goal would be exposing these hooks experimentally to everyone! We can move from there as people start extending their schemas and find out what we need. We can introduce these hooks under an experimental_hooks property (I’m taking the _ convention from React 😉). We can document these hooks but with the warning that we reserve the right to make breaking changes in non-major releases until they are stabilized. What do you think about that?

To expose these hooks would actually be pretty simple, so if anyone wants to open a PR, let’s discuss!

@cellis
Copy link
Author

cellis commented Mar 5, 2017

@calebmer As it stands, the lack of hooks is the only thing preventing quite a few people using this library. Exposing them and then deprecating in the next version would be much better than not. Perhaps I will open a PR, but no promises, as I've already signed up to do quite a few other graphql libraries ;).

@cellis cellis closed this as completed Mar 5, 2017
@benjie
Copy link
Member

benjie commented Mar 5, 2017

👍

@benjie benjie reopened this Mar 5, 2017
@benjie benjie changed the title How to write queries that depend on external data? Expose hooks to enable extending the GraphQL schema Mar 5, 2017
@mikeburgh
Copy link

I have spent some time getting custom mutations working.. following the advice of @calebmer I used the _hooks and added a new one for custom mutations.. which is picked up in createMutationGqlType and added to the mutation for GraphQLObjectType.

What are the thoughts on storing the mutations and then injecting them.. currently I am thinking about adding a config/CLI option to pass in a path, and the files in that path would be imported and added ?

Each file would be required to export a GraphQL schema object, something along the lines of:

{"testMutation": {
            args: {
                input: {
                    description:'the input'
                    ,type: new graphql.GraphQLNonNull(graphql.GraphQLString)
                }
            }
            ,description:"test Mutation injection"
            ,resolve: function(_source,args, context, info) {
                 return  {i:args.input,t:'re'};
            }
            ,type: new GraphQLObjectType({
                name: "testPayload"
                ,description: "The output of our  mutation."
                ,fields: {
                    i: { type: graphql.GraphQLString, description: 'Something out' },
                    t: { type: graphql.GraphQLString, description: 'Something else out' },
                }
                }
            )
        }
}

My TypeScript knowledge is 0, so for now I have done this with the compiled JS code.. so I still need to port it back into TypeScript and submit a PR.. pending the feedback.

The other aspect I want to solve is hooking into a mutation that is created by PostgrapQL prior to it being returned to the client (for post processing work etc).

@benjie
Copy link
Member

benjie commented Jul 9, 2017

v4 adds a plugin architecture to the generated schema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants