Improve introspection types + new getIntrospectionQuery()

This adds a new function `getIntrospectionQuery()` which allows for some minor configuration over the resulting query text: to exclude descriptions if your use case does not require them.

ref: graphql/graphql-js#1113
This commit is contained in:
Daniel Tschinder 2018-02-11 18:19:52 +01:00
parent 6e358eb26c
commit ff63e07b05
4 changed files with 23 additions and 99 deletions

View File

@ -37,11 +37,25 @@ class Introspection
private static $map = []; private static $map = [];
/** /**
* Options:
* - descriptions
* Whether to include descriptions in the introspection result.
* Default: true
*
* @param array $options
* @return string * @return string
*/ */
public static function getIntrospectionQuery($includeDescription = true) public static function getIntrospectionQuery($options = [])
{ {
$withDescription = <<<'EOD' if (is_bool($options)) {
trigger_error('Calling Introspection::getIntrospectionQuery(boolean) is deprecated. Please use Introspection::getIntrospectionQuery(["descriptions" => boolean]).', E_USER_DEPRECATED);
$descriptions = $options;
} else {
$descriptions = !array_key_exists('descriptions', $options) || $options['descriptions'] === true;
}
$descriptionField = $descriptions ? 'description' : '';
return <<<EOD
query IntrospectionQuery { query IntrospectionQuery {
__schema { __schema {
queryType { name } queryType { name }
@ -52,7 +66,7 @@ class Introspection
} }
directives { directives {
name name
description {$descriptionField}
locations locations
args { args {
...InputValue ...InputValue
@ -64,10 +78,10 @@ class Introspection
fragment FullType on __Type { fragment FullType on __Type {
kind kind
name name
description {$descriptionField}
fields(includeDeprecated: true) { fields(includeDeprecated: true) {
name name
description {$descriptionField}
args { args {
...InputValue ...InputValue
} }
@ -85,7 +99,7 @@ class Introspection
} }
enumValues(includeDeprecated: true) { enumValues(includeDeprecated: true) {
name name
description {$descriptionField}
isDeprecated isDeprecated
deprecationReason deprecationReason
} }
@ -96,7 +110,7 @@ class Introspection
fragment InputValue on __InputValue { fragment InputValue on __InputValue {
name name
description {$descriptionField}
type { ...TypeRef } type { ...TypeRef }
defaultValue defaultValue
} }
@ -134,95 +148,6 @@ class Introspection
} }
} }
EOD; EOD;
$withoutDescription = <<<'EOD'
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
fields(includeDeprecated: true) {
name
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
EOD;
return $includeDescription ? $withDescription : $withoutDescription;
} }
public static function getTypes() public static function getTypes()

View File

@ -1,7 +1,6 @@
<?php <?php
namespace GraphQL\Tests\Type; namespace GraphQL\Tests\Type;
use GraphQL\Error\Error;
use GraphQL\GraphQL; use GraphQL\GraphQL;
use GraphQL\Language\SourceLocation; use GraphQL\Language\SourceLocation;
use GraphQL\Schema; use GraphQL\Schema;

View File

@ -28,7 +28,7 @@ class IntrospectionTest extends \PHPUnit_Framework_TestCase
]) ])
]); ]);
$request = Introspection::getIntrospectionQuery(false); $request = Introspection::getIntrospectionQuery(['descriptions' => false]);
$expected = array ( $expected = array (
'data' => 'data' =>
array ( array (

View File

@ -53,7 +53,7 @@ abstract class AbstractQuerySecurityTest extends \PHPUnit_Framework_TestCase
protected function assertIntrospectionQuery($maxExpected) protected function assertIntrospectionQuery($maxExpected)
{ {
$query = Introspection::getIntrospectionQuery(true); $query = Introspection::getIntrospectionQuery();
$this->assertMaxValue($query, $maxExpected); $this->assertMaxValue($query, $maxExpected);
} }