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

Create helper functions for extending queries and mutations #246

Closed
CodeProKid opened this issue Oct 10, 2017 · 5 comments
Closed

Create helper functions for extending queries and mutations #246

CodeProKid opened this issue Oct 10, 2017 · 5 comments
Labels
component: architecture Relating to fundamental architectural decisions needs: discussion Requires a discussion to proceed type: enhancement Improvements to existing functionality
Milestone

Comments

@CodeProKid
Copy link
Member

I think it would be good to have some helper functions that make it easier for developers to extend WPGraphQL. Something similar to register_rest_route(). This would help lower the barrier to entry for the community, and would also provide developers with a consistent way to extend the plugin, which would make backwards compatibility easier for us down the line.

This is just a stubbed issue, I have some more in depth ideas for this that I'll share when I get a moment, open to feedback from anyone else as well though.

@CodeProKid CodeProKid added component: architecture Relating to fundamental architectural decisions type: enhancement Improvements to existing functionality needs: discussion Requires a discussion to proceed labels Oct 10, 2017
@CodeProKid CodeProKid added this to the 1.0 release milestone Oct 10, 2017
@jasonbahl
Copy link
Collaborator

@CodeProKid ya, I'd love to see what ideas you have. I think this would definitely be a good thing to have some nice register_* functions (and other general "access functions / template tags") to make it easier to hook into the schema.

Off the top of my head, here are some that I can think of that might be good:

register_graphql_field

This would allow someone to add a field to any type in the Schema.

Might look like register_graphql_field( $types $args )

Where $type is the Type (or array of Types?) in the Schema to add the field to, and $args would include the name of the field, the description, field args, and the resolver for the function.

$args = [
  'name' => 'myCustomField',
  'description' => __( 'My Custom Field for Posts', 'my-plugin' ),
  'resolve' => function( $post, $args, $context, $info  ) {
      return get_post_meta( $post->ID, 'my_custom_field_key', true );
  }
];

register_graphql_field( [ 'Post', $args ] );

register_graphql_type

This one I think needs a little more thought. . .because there are different Types that can be registered, so not sure if we want a different function for each, like register_graphql_enum_type and register_graphql_object_type or something like that? Or if we can make register_graphql_type work for all with the proper input $args. . .

register_graphql_mutation_field / register_graphql_mutation_type?

Not sure what this would look like, but might be helpful. . .

graphql_wrap_resolver( $fieldName, $type, $callback )

Not sure exactly how this should look, but I think it would be good to have some function like this that allows folks to add granular control over specific fields. . .

For example, if I wanted to hide just privateField on the postObject, I could do something like:

graphql_wrap_resolver( 'privateField', 'Post', function( $source, $args, $context, $info, $defaultResolver ) {
    if ( ! current_user_can( 'seePrivateFields' ) {
        throw new \Exception( __( 'Sorry, you cannot see private fields', 'wp-graphql' ) );
    }
    return $defaultResolver;
});

we could even potentially have other pre-made ones, like: graphql_require_authentication( $fieldName, $type ) that is potentially implemented something to this tune. . .(totally pseudo code):

function graphql_require_auth( $fieldName, $type ) {
  return graphql_wrap_resolver( $fieldName, $type, 'requireAuthCallback' );
}

function requireAuthCallback( $source, $args, $context, $info, $defaultResolver ) {
  if ( ! wp_get_current_user() ) {
    throw new \Exception( __( 'You must be logged in to do this', 'wp-graphql' ) );
  }
  return $defaultResolver;
}

@cr101
Copy link

cr101 commented Oct 11, 2017

Would it be possible to add my CPTs and their custom fields to the Schema and then run a query like below?

query {
	author(id: 4452) {
		id
		name,
		books(isbn: a45459) {
			title,
			isbn
		}
	}
}

I'm using ACF but I've registered all my custom post types and the custom fields for each CPT via PHP instead of the ACF UI. All my CPTs are linked to each other via bidirectional relationships.

@jasonbahl
Copy link
Collaborator

Hey @cr101 I’m afk but will get you more details when I get to a computer. You can search closed issues though and you should be able to find info on registering custom post types and adding fields to the types in your schema.

@cr101
Copy link

cr101 commented Oct 11, 2017

Hi @jasonbahl

I’m afk but will get you more details when I get to a computer.

I'd love to see an example, thank you. I had a look on the Wiki but I couldn't find any examples.

@CodeProKid CodeProKid modified the milestones: 1.0 release, 1.1 Release Oct 17, 2017
@CodeProKid CodeProKid modified the milestones: 1.1 Release, 0.1.0 Sep 4, 2018
@jasonbahl
Copy link
Collaborator

closing this and tracking on #327

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: architecture Relating to fundamental architectural decisions needs: discussion Requires a discussion to proceed type: enhancement Improvements to existing functionality
Projects
None yet
Development

No branches or pull requests

3 participants