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

Add GraphQL Support #3149

Open
Shelob9 opened this Issue Apr 5, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@Shelob9
Copy link
Collaborator

Shelob9 commented Apr 5, 2019

No description provided.

@Shelob9 Shelob9 added the enhancement label Apr 5, 2019

@jasonbahl

This comment has been minimized.

Copy link

jasonbahl commented Apr 5, 2019

@Shelob9 here's a rough initial concept for how forms could be supported. Very rough, of course, but should get you running:

add_action( 'graphql_register_types', function() {

	register_graphql_object_type( 'CalderaTextField', [
		'fields' => [
			'label' => [
				'type' => 'String',
			],
			'description' => [
				'type' => 'String',
			],
			'placeholder' => [
				'type' => 'String',
			],
			'value' => [
				'type' => 'String',
			],
		],
	] );

	register_graphql_object_type( 'CalderaNumberField', [
		'fields' => [
			'label' => [
				'type' => 'String',
			],
			'description' => [
				'type' => 'String',
			],
			'placeholder' => [
				'type' => 'String',
			],
			'value' => [
				'type' => 'Float',
			],
		],
	] );

	register_graphql_object_type( 'CalderaSelectField', [
		'fields' => [
			'label' => [
				'type' => 'String',
			],
			'description' => [
				'type' => 'String',
			],
			'placeholder' => [
				'type' => 'String',
			],
			'value' => [
				'type' => 'String',
			],
		],
	] );

	$form_types = [
		'CalderaSelectField' => \WPGraphQL\TypeRegistry::get_type( 'CalderaSelectField' ),
		'CalderaNumberField' => \WPGraphQL\TypeRegistry::get_type( 'CalderaNumberField' ),
		'CalderaTextField' => \WPGraphQL\TypeRegistry::get_type( 'CalderaTextField' ),
	];

	register_graphql_union_type( 'CalderaFormFieldsUnion', [
		'types' => $form_types,
		'resolveType' => function( $field ) {
			$type = null;
			switch( $field['type'] ) {
				case 'Text':
					$type = \WPGraphQL\TypeRegistry::get_type( 'CalderaTextField' );
					break;
				case 'Select':
					$type = \WPGraphQL\TypeRegistry::get_type( 'CalderaSelectField' );
					break;
				case 'Number':
					$type = \WPGraphQL\TypeRegistry::get_type( 'CalderaNumberField' );
					break;
			}
			return $type;
		}
	]);

	register_graphql_field( 'RootQuery', 'calderaForm', [
		'type' => [ 'list_of' => 'CalderaFormFieldsUnion' ],
		'resolve' => function() {



			// Get the form from caldera
			$fields = [
				[
					'type' => 'Number',
					'label' => 'User created label',
				],
				[
					'type' => 'Select'
				],
				[
					'type' => 'Text'
				],
			];
			return $fields;
		}
	] );

} );

That would allow a query like the following:

{
  calderaForm {
    __typename
    ... on CalderaNumberField {
      label
      description
    }
    ... on CalderaSelectField {
      label
    }
    ... on CalderaTextField {
      label
    }
  }
}

And return data like so:

{
  "data": {
    "calderaForm": [
      {
        "__typename": "CalderaNumberField",
        "label": "User created label",
        "description": null
      },
      {
        "__typename": "CalderaSelectField",
        "label": null
      },
      {
        "__typename": "CalderaTextField",
        "label": null
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.