Skip to content

Add Pointer support to GraphQL - #114

Closed
bbuck wants to merge 1 commit into
graphql-go:masterfrom
bbuck:pointers
Closed

bbuck wants to merge 1 commit into
graphql-go:masterfrom
bbuck:pointers

Conversation

@bbuck

@bbuck bbuck commented Feb 15, 2016

Copy link
Copy Markdown
Collaborator

This adds a new meta-type, Pointer to support pointer fields in structs. This allows easily converting a pattern like:

"ptr_field": &graphql.Field{
        Type: graphql.String,
        Resolve: func(params graphql.ResolveParams) (interface{}, error) {
                source := params.Source.(*SomeType)

                return *source.PtrField
        },
},

To simply:

"ptr_field": &graphql.Field{
        Type: graphql.NewPointer(graphql.String),
},

I realize that there is no spec for pointers but pointers aren't part of every language. I think it's important that we allow support for this due to significant amounts of boilerplate for simple pointer fields. I don't think there is really a better way to represent this outside of creating this new type, but I feel like it's an elegant solution.

This PR would close #101

@sogko

sogko commented Mar 2, 2016

Copy link
Copy Markdown
Member

Hi @bbuck

Thanks for the PR, really appreciate the effort you contributed to the project.

Currently I have reservations about introducing a new type to graphql-go that goes beyond the current published spec. GraphQL is still very much in its early infancy and things does get change pretty quickly (we barely have enough time to achieve parity with graphql-js at the moment lol).

I think currently #101 fits the use case for a user-defined Scalar type. This means that the user can create a new Scalar type named Pointer through existing API graphql.NewScalar(), similar how built-in types like Float, String and Int are defined in the library.
(Refer to: https://github.com/graphql-go/graphql/blob/master/scalars.go)

If anyone need a complete example of how to implement a custom scalar type using graphql.NewScalar(), I can spend some time writing that a gist for it.

Thanks again!

Edit: I'm not sure if you have already tried the graphql.NewScalar() approach. If you have already, maybe you could share issues and obstacles that you had faced when trying to implement the Pointer type.

@zbintliff

zbintliff commented May 31, 2017

Copy link
Copy Markdown

This was superseded by #199

This was done without introducing a new point, but accepting pointers or scalars and coercing them correctly.

@bbuck

bbuck commented May 31, 2017

Copy link
Copy Markdown
Collaborator Author

This issue needs to be closed, you are correct. The intent here was to do this explicitly, instructing those viewing your Schema that a pointer is expected to be returned. I'm not opposed to the solution presented in #199 though, as I just wanted the problem to be resolved!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How should pointers to standard types be handled when no Resolve is defined?

4 participants