├── graphql_examples.info.yml └── src └── Plugin └── GraphQL ├── InputTypes └── ArticleInput.php └── Mutations ├── CreateArticle.php ├── DeleteArticle.php ├── FileUpload.php └── UpdateArticle.php /graphql_examples.info.yml: -------------------------------------------------------------------------------- 1 | name: GraphQL Examples 2 | type: module 3 | description: 'GraphQL implementation examples.' 4 | package: GraphQL 5 | core: 8.x 6 | dependencies: 7 | - graphql_core 8 | -------------------------------------------------------------------------------- /src/Plugin/GraphQL/InputTypes/ArticleInput.php: -------------------------------------------------------------------------------- 1 | $args['input']['title'], 38 | 'body' => $args['input']['body'], 39 | ]; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/Plugin/GraphQL/Mutations/DeleteArticle.php: -------------------------------------------------------------------------------- 1 | entityTypeManager = $entityTypeManager; 77 | $this->currentUser = $currentUser; 78 | $this->mimeTypeGuesser = $mimeTypeGuesser; 79 | $this->fileSystem = $fileSystem; 80 | } 81 | 82 | /** 83 | * {@inheritdoc} 84 | */ 85 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { 86 | return new static( 87 | $configuration, 88 | $pluginId, 89 | $pluginDefinition, 90 | $container->get('entity_type.manager'), 91 | $container->get('current_user'), 92 | $container->get('file.mime_type.guesser'), 93 | $container->get('file_system') 94 | ); 95 | } 96 | 97 | /** 98 | * {@inheritdoc} 99 | */ 100 | public function resolve($value, array $args, ResolveInfo $info) { 101 | /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ 102 | $file = $args['file']; 103 | 104 | // Check for file upload errors and return FALSE for this file if a lower 105 | // level system error occurred. 106 | // 107 | // @see http://php.net/manual/features.file-upload.errors.php. 108 | switch ($file->getError()) { 109 | case UPLOAD_ERR_INI_SIZE: 110 | case UPLOAD_ERR_FORM_SIZE: 111 | return new EntityCrudOutputWrapper(NULL, NULL, [ 112 | $this->t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', [ 113 | '%file' => $file->getFilename(), 114 | '%maxsize' => format_size(file_upload_max_size()) 115 | ]), 116 | ]); 117 | 118 | case UPLOAD_ERR_PARTIAL: 119 | case UPLOAD_ERR_NO_FILE: 120 | return new EntityCrudOutputWrapper(NULL, NULL, [ 121 | $this->t('The file %file could not be saved because the upload did not complete.', [ 122 | '%file' => $file->getFilename(), 123 | ]), 124 | ]); 125 | 126 | case UPLOAD_ERR_OK: 127 | // Final check that this is a valid upload, if it isn't, use the 128 | // default error handler. 129 | if (is_uploaded_file($file->getRealPath())) { 130 | break; 131 | } 132 | 133 | // Unknown error. 134 | default: 135 | return new EntityCrudOutputWrapper(NULL, NULL, [ 136 | $this->t('The file %file could not be saved. An unknown error has occurred.', [ 137 | '%file' => $file->getFilename(), 138 | ]), 139 | ]); 140 | } 141 | 142 | $name = $file->getClientOriginalName(); 143 | $mime = $this->mimeTypeGuesser->guess($name); 144 | $destination = file_destination("public://{$file->getFilename()}", FILE_EXISTS_RENAME); 145 | 146 | // Begin building file entity. 147 | $values = [ 148 | 'uid' => $this->currentUser->id(), 149 | 'status' => 0, 150 | 'filename' => $name, 151 | 'uri' => $destination, 152 | 'filesize' => $file->getSize(), 153 | 'filemime' => $mime, 154 | ]; 155 | 156 | $storage = $this->entityTypeManager->getStorage('file'); 157 | /** @var \Drupal\file\FileInterface $entity */ 158 | $entity = $storage->create($values); 159 | 160 | // Validate the entity values. 161 | if (($violations = $entity->validate()) && $violations->count()) { 162 | return new EntityCrudOutputWrapper(NULL, $violations); 163 | } 164 | 165 | // Validate the file name length. 166 | if ($errors = file_validate($entity, ['file_validate_name_length' => []])) { 167 | return new EntityCrudOutputWrapper(NULL, NULL, [ 168 | $this->t('The specified file %name could not be uploaded.', [ 169 | '%file' => $file->getFilename(), 170 | ]), 171 | ]); 172 | } 173 | 174 | // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary 175 | // directory. This overcomes open_basedir restrictions for future file 176 | // operations. 177 | if (!$this->fileSystem->moveUploadedFile($file->getRealPath(), $entity->getFileUri())) { 178 | return new EntityCrudOutputWrapper(NULL, NULL, [ 179 | $this->t('Could not move uploaded file %name.', [ 180 | '%file' => $file->getFilename(), 181 | ]), 182 | ]); 183 | } 184 | 185 | // Set the permissions on the new file. 186 | $this->fileSystem->chmod($entity->getFileUri()); 187 | 188 | // If we reached this point, we can save the file. 189 | if (($status = $entity->save()) && $status === SAVED_NEW) { 190 | return new EntityCrudOutputWrapper($entity); 191 | } 192 | 193 | return NULL; 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /src/Plugin/GraphQL/Mutations/UpdateArticle.php: -------------------------------------------------------------------------------- 1 | $args['input']['title'], 39 | 'body' => $args['input']['body'], 40 | ]); 41 | } 42 | 43 | } 44 | --------------------------------------------------------------------------------