Wp-graphql: WP-GraphQl stops end-point when adding WP-GraphQl options to Custom Post Types.

Created on 13 Jul 2017  路  5Comments  路  Source: wp-graphql/wp-graphql

Hi! I'm trying connect custom post types with WP-GraphQl with this instruction:
https://edwincromley.gitbooks.io/wp-graphql/content/handling-custom-post-types.html

After this connection to end-point stops and GraphiQL Chrome plugin Crushes. Havin' a lot of errors of React and HTTP-500 from server.

Can you help me with this?

Most helpful comment

@TimurGumarov Thanks for your interest in the plugin!

Until we have more time to get some official docs together (hopefully soon, and help appreciated 馃槈 ), this snippet should help:

Let's say you have a foo post_type registered. You can add it to your GraphQL schema like so:

add_action( 'do_graphql_request', function() {

    global $wp_post_types;

    if ( isset( $wp_post_types['foo'] ) ) {
        $wp_post_types['foo']->show_in_graphql     = true;
        $wp_post_types['foo']->graphql_single_name = 'foo';
        $wp_post_types['foo']->graphql_plural_name = 'foos';
    }

} );

This will allow you to query it like you can query posts, so something like:

{
  foos{
    edges{
      node{
        id
        title
        slug
      }
    }
  }
}

Now, let's say your foo post type had a color field on it that you wanted to be part of the schema. You could add that like so:

add_filter( 'graphql_foo_fields', 'your_callback_function' );

function your_callback_function( $fields ) {

    $fields['color'] = [
        'type' => 'String',
        'description' => __( 'The color of the post', 'my-graphql-extension-namespace' ),
        'resolve' => function( \WP_Post $foo, array $args, $context, $info ) {
            $color = get_post_meta( $foo->ID, 'color', true );
            return ( ! empty( $color ) && is_string( $color ) ) ? $color : null;
        },
    ];

    return $fields;

}

Then you can query for that field on the foo type in your schema, like so:

{
  foos{
    edges{
      node{
        id
        color
      }
    }
  }
}

There's also more info about extending the schema in the comments on these issues:
https://github.com/wp-graphql/wp-graphql/issues/118
https://github.com/wp-graphql/wp-graphql/issues/11

All 5 comments

Hi @TimurGumarov,

The link to that gitbook has nothing to do with this repo. That is a completely different plugin. I will take down that gitbook.

Not sure if @jasonbahl has new docs up. At some point soonish I hope to have some free time to be able to write documentation for the new WP GraphQL.

@TimurGumarov Thanks for your interest in the plugin!

Until we have more time to get some official docs together (hopefully soon, and help appreciated 馃槈 ), this snippet should help:

Let's say you have a foo post_type registered. You can add it to your GraphQL schema like so:

add_action( 'do_graphql_request', function() {

    global $wp_post_types;

    if ( isset( $wp_post_types['foo'] ) ) {
        $wp_post_types['foo']->show_in_graphql     = true;
        $wp_post_types['foo']->graphql_single_name = 'foo';
        $wp_post_types['foo']->graphql_plural_name = 'foos';
    }

} );

This will allow you to query it like you can query posts, so something like:

{
  foos{
    edges{
      node{
        id
        title
        slug
      }
    }
  }
}

Now, let's say your foo post type had a color field on it that you wanted to be part of the schema. You could add that like so:

add_filter( 'graphql_foo_fields', 'your_callback_function' );

function your_callback_function( $fields ) {

    $fields['color'] = [
        'type' => 'String',
        'description' => __( 'The color of the post', 'my-graphql-extension-namespace' ),
        'resolve' => function( \WP_Post $foo, array $args, $context, $info ) {
            $color = get_post_meta( $foo->ID, 'color', true );
            return ( ! empty( $color ) && is_string( $color ) ) ? $color : null;
        },
    ];

    return $fields;

}

Then you can query for that field on the foo type in your schema, like so:

{
  foos{
    edges{
      node{
        id
        color
      }
    }
  }
}

There's also more info about extending the schema in the comments on these issues:
https://github.com/wp-graphql/wp-graphql/issues/118
https://github.com/wp-graphql/wp-graphql/issues/11

@jasonbahl and @BE-Webdesign Thank you for response!

As this thread has been used few times as a reference I'm leaving a little notice for a deprecated syntax.

The following doesn't work anymore.

'type' => WPGraphQL\Types::string(),

If you look at the guidelines the correct syntax is the one below:

'type' => 'String'

@a-barbieri I updated my comment above to reflect your suggested change as well. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benknight picture benknight  路  3Comments

saulirajala picture saulirajala  路  3Comments

BE-Webdesign picture BE-Webdesign  路  3Comments

jasonbahl picture jasonbahl  路  4Comments

cr101 picture cr101  路  4Comments