dre1080/wp-graphql-upload

Adds file upload support to the WP GraphQL Plugin.

Installs: 22 222

Dependents: 0

Suggesters: 0

Security: 0

Stars: 32

Watchers: 3

Forks: 3

Open Issues: 1

Type:wordpress-plugin

v0.1.5 2023-07-17 11:43 UTC

This package is auto-updated.

Last update: 2024-03-17 16:55:31 UTC


README

This plugin adds Upload support to the WPGraphQL plugin as specified by graphql-multipart-request-spec.

Requirements

  • PHP >= 7.1
  • WordPress >= 5.0
  • WPGraphQL >= 1.0.0

Activating / Using

You can install and activate the plugin like any WordPress plugin. Download the .zip from Github and add to your plugins directory, then activate.

Once the plugin is active, the Upload scalar type will be available to your mutation input fields.

If you're using composer:

composer require dre1080/wp-graphql-upload

Usage

Then you can start using in your mutations like so:

register_graphql_mutation(
  'upload', [
      'inputFields' => [
          'file' => [
              'type' => ['non_null' => 'Upload'],
          ],
      ],
      'outputFields' => [
          'text' => [
              'type'    => 'String',
              'resolve' => function ($payload) {
                  return $payload['text'];
              },
          ],
      ],
      'mutateAndGetPayload' => function ($input) {
          if (!function_exists('wp_handle_sideload')) {
              require_once(ABSPATH . 'wp-admin/includes/file.php');
          }

          wp_handle_sideload($input['file'], [
              'test_form' => false,
              'test_type' => false,
          ]);

          return [
              'text' => 'Uploaded file was "' . $input['file']['name'] . '" (' . $input['file']['type'] . ').',
          ];
      }
  ]
);

Testing

Requirements:

To run the tests, run the following commands:

bin/install-wp-tests.sh
vendor/bin/phpunit