From 1e6f18caace91e05c82bee40d56dcaf3c76f080c Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Thu, 3 Oct 2019 18:10:00 -0400 Subject: [PATCH 1/5] Create modifying-automatic-field-args.md --- src/content/guides/modifying-automatic-field-args.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/content/guides/modifying-automatic-field-args.md diff --git a/src/content/guides/modifying-automatic-field-args.md b/src/content/guides/modifying-automatic-field-args.md new file mode 100644 index 0000000..7ccc3cd --- /dev/null +++ b/src/content/guides/modifying-automatic-field-args.md @@ -0,0 +1,5 @@ +# Modifying the arguments of an automatically registered Root field + +It is not possible to amend the arguments of an automatically registered root field, such as the _By_ field registered when adding a custom post type. + +Instead, this field needds to be From 4b4f5ab361a90e3f3831b5a3c724d5ea585d8a4c Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Thu, 3 Oct 2019 18:11:51 -0400 Subject: [PATCH 2/5] Update modifying-automatic-field-args.md --- src/content/guides/modifying-automatic-field-args.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/guides/modifying-automatic-field-args.md b/src/content/guides/modifying-automatic-field-args.md index 7ccc3cd..efc473e 100644 --- a/src/content/guides/modifying-automatic-field-args.md +++ b/src/content/guides/modifying-automatic-field-args.md @@ -2,4 +2,4 @@ It is not possible to amend the arguments of an automatically registered root field, such as the _By_ field registered when adding a custom post type. -Instead, this field needds to be +Instead, this field needs to first be _deregistered_ then registered again with the desired arguments. From 2614e05ef29a28d4f804fbaa0b4c61f6a9755bae Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Mon, 7 Oct 2019 17:24:05 -0400 Subject: [PATCH 3/5] Update modifying-automatic-field-args.md --- src/content/guides/modifying-automatic-field-args.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/guides/modifying-automatic-field-args.md b/src/content/guides/modifying-automatic-field-args.md index efc473e..142cb75 100644 --- a/src/content/guides/modifying-automatic-field-args.md +++ b/src/content/guides/modifying-automatic-field-args.md @@ -1,5 +1,5 @@ # Modifying the arguments of an automatically registered Root field -It is not possible to amend the arguments of an automatically registered root field, such as the _By_ field registered when adding a custom post type. +It is not possible to amend the arguments of an automatically registered root field, such as the _By_ field registered when adding a custom post type (e.g. `forBarBy` for type `fooBar`). -Instead, this field needs to first be _deregistered_ then registered again with the desired arguments. +Instead, this field needs to first be _[deregistered](https://github.com/wp-graphql/wp-graphql/blob/develop/access-functions.php#L164)_ then registered again with the desired arguments. From 385cfc26c8a605069c05795f2e8b18b6bfe53162 Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Mon, 7 Oct 2019 18:16:08 -0400 Subject: [PATCH 4/5] Update modifying-automatic-field-args.md --- .../guides/modifying-automatic-field-args.md | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/content/guides/modifying-automatic-field-args.md b/src/content/guides/modifying-automatic-field-args.md index 142cb75..f78c6c5 100644 --- a/src/content/guides/modifying-automatic-field-args.md +++ b/src/content/guides/modifying-automatic-field-args.md @@ -2,4 +2,39 @@ It is not possible to amend the arguments of an automatically registered root field, such as the _By_ field registered when adding a custom post type (e.g. `forBarBy` for type `fooBar`). -Instead, this field needs to first be _[deregistered](https://github.com/wp-graphql/wp-graphql/blob/develop/access-functions.php#L164)_ then registered again with the desired arguments. +Instead, this field needs to first be _[deregistered](https://github.com/wp-graphql/wp-graphql/blob/develop/access-functions.php#L164)_ then registered again with the desired arguments. + +Deregistering a field is as simple as `deregister_graphql_field( $type_name, $field_name )` where `$type_name` is the name of the Type to remove the field from (e.g. `RootQuery`) and $field_name is the name of the field to remove (e.g. `forBarBy`). + +Once the automatically registered field has been deregistered, it can be registered again with the desired args. In the below example, we are only supporting the _slug_ arg, and are adding a _password_ arg. + +``` +register_graphql_field( + 'RootQuery', 'fooBarBy', [ + 'args' => [ + 'password' => [ + 'description' => __( 'The password required for fetching a Foo Bar.', 'guggenheim' ), + 'type' => 'String', + ], + 'slug' => [ + 'description' => __( 'Get the Foo Bar by its slug.', 'guggenheim' ), + 'type' => 'String', + ], + ], + 'description' => __( 'A Foo Bar object.', 'guggenheim' ), + 'type' => 'fooBar', + 'resolve' => function( $post, $args, $context, $info ) { + if ( ! check_press_password( $args['password'] ) ) { + return null; + } + + $post = get_page_by_path( $args['slug'], OBJECT, 'foo_bar' ); + if ( $post->post_type !== 'foo_bar' || $post->ID === 0 ) { + throw new UserError( sprintf( __( 'No %1$s exists with slug %2$s' ), 'fooBar', $args['slug'] ) ); + } + + return $post ? new \WPGraphQL\Model\Post( $post ) : null; + }, + ] +); +``` From bcd16822aff3d2628b0bf0e81d57f72c314c1d70 Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Mon, 7 Oct 2019 18:25:48 -0400 Subject: [PATCH 5/5] Update modifying-automatic-field-args.md --- src/content/guides/modifying-automatic-field-args.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content/guides/modifying-automatic-field-args.md b/src/content/guides/modifying-automatic-field-args.md index f78c6c5..f3be4d1 100644 --- a/src/content/guides/modifying-automatic-field-args.md +++ b/src/content/guides/modifying-automatic-field-args.md @@ -8,6 +8,8 @@ Deregistering a field is as simple as `deregister_graphql_field( $type_name, $fi Once the automatically registered field has been deregistered, it can be registered again with the desired args. In the below example, we are only supporting the _slug_ arg, and are adding a _password_ arg. +For a more complete example, and a great starting point, take a look at the core `By` function at [src/Type/Object/RootQuery.php](https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/Object/RootQuery.php#L209) + ``` register_graphql_field( 'RootQuery', 'fooBarBy', [