mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 21:06:05 +03:00
424 lines
11 KiB
HTML
Executable File
424 lines
11 KiB
HTML
Executable File
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
|
|
|
<title>Introduction - graphql-php</title>
|
|
|
|
|
|
<link rel="shortcut icon" href="../img/favicon.ico">
|
|
|
|
|
|
<link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
|
|
|
|
<link rel="stylesheet" href="../css/theme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
|
|
<link rel="stylesheet" href="../css/highlight.css">
|
|
|
|
|
|
<script>
|
|
// Current page data
|
|
var mkdocs_page_name = "Introduction";
|
|
var mkdocs_page_input_path = "type-system\index.md";
|
|
var mkdocs_page_url = "/type-system/";
|
|
</script>
|
|
|
|
<script src="../js/jquery-2.1.1.min.js"></script>
|
|
<script src="../js/modernizr-2.8.3.min.js"></script>
|
|
<script type="text/javascript" src="../js/highlight.pack.js"></script>
|
|
<script src="../js/theme.js"></script>
|
|
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
<div class="wy-grid-for-nav">
|
|
|
|
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
|
|
<div class="wy-side-nav-search">
|
|
<a href=".." class="icon icon-home"> graphql-php</a>
|
|
<div role="search">
|
|
<form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
<ul class="current">
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="..">About</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../getting-started/">Getting Started</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<ul class="subnav">
|
|
<li><span>Type System</span></li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 current">
|
|
<a class="current" href="./">Introduction</a>
|
|
|
|
<ul>
|
|
|
|
<li class="toctree-l3"><a href="#type-system">Type System</a></li>
|
|
|
|
|
|
<li class="toctree-l3"><a href="#type-definition-styles">Type Definition Styles</a></li>
|
|
|
|
|
|
<li class="toctree-l3"><a href="#type-registry">Type Registry</a></li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="object-types/">Object Types</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="scalar-types/">Scalar Types</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="enum-types/">Enumeration Types</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="lists-and-nonnulls/">Lists and Non-Null</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="interfaces/">Interfaces</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="unions/">Unions</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="input-types/">Input Types</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="directives/">Directives</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="schema/">Schema</a>
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../executing-queries/">Executing Queries</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../error-handling/">Handling Errors</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../data-fetching/">Fetching Data</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../best-practices/">Best Practices</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
<li>
|
|
<li class="toctree-l1 ">
|
|
<a class="" href="../complementary-tools/">Complementary Tools</a>
|
|
|
|
</li>
|
|
<li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="..">graphql-php</a>
|
|
</nav>
|
|
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="rst-content">
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
<ul class="wy-breadcrumbs">
|
|
<li><a href="..">Docs</a> »</li>
|
|
|
|
|
|
|
|
<li>Type System »</li>
|
|
|
|
|
|
|
|
<li>Introduction</li>
|
|
<li class="wy-breadcrumbs-aside">
|
|
|
|
</li>
|
|
</ul>
|
|
<hr/>
|
|
</div>
|
|
<div role="main">
|
|
<div class="section">
|
|
|
|
<h1 id="type-system">Type System</h1>
|
|
<p>To start using GraphQL you are expected to implement a type hierarchy and expose it as <a href="../type-system/schema/">Schema</a>. </p>
|
|
<p>In <strong>graphql-php</strong> <code>type</code> is an instance of internal class from
|
|
<code>GraphQL\Type\Definition</code> namespace: <code>ScalarType</code>, <code>ObjectType</code>, <code>InterfaceType</code>,
|
|
<code>UnionType</code>, <code>InputObjectType</code> (or one of it's subclasses).</p>
|
|
<p>But most of the types in your schema will be <a href="../type-system/object-types/">object types</a>.</p>
|
|
<h1 id="type-definition-styles">Type Definition Styles</h1>
|
|
<p>Several styles of type definitions are supported depending on your preferences.</p>
|
|
<p>Inline definitions:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
$myType = new ObjectType([
|
|
'name' => 'MyType',
|
|
'fields' => [
|
|
'id' => Type::id()
|
|
]
|
|
]);
|
|
</code></pre>
|
|
|
|
<p>Class per type, using inheritance:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
class MyType extends ObjectType
|
|
{
|
|
public function __construct()
|
|
{
|
|
$config = [
|
|
// Note: 'name' is not needed in this form:
|
|
// it will be inferred from class name by omitting namespace and dropping "Type" suffix
|
|
'fields' => [
|
|
'id' => Type::id()
|
|
]
|
|
];
|
|
parent::__construct($config);
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Class per type, using composition:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
use GraphQL\Type\Definition\Type;
|
|
use GraphQL\Type\DefinitionContainer;
|
|
|
|
class MyType implements DefinitionContainer
|
|
{
|
|
private $definition;
|
|
|
|
public function getDefinition()
|
|
{
|
|
return $this->definition ?: ($this->definition = new ObjectType([
|
|
'name' => 'MyType',
|
|
'fields' => [
|
|
'id' => Type::id()
|
|
]
|
|
]));
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<p>You can also mix-and-match styles for convenience. For example:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
class BlogPostType extends ObjectType
|
|
{
|
|
public function __construct()
|
|
{
|
|
$config = [
|
|
'fields' => [
|
|
'body' => new ObjectType([
|
|
'name' => 'BlogPostBody',
|
|
'fields' => [
|
|
'html' => Type::string(),
|
|
'text' => Type::string(),
|
|
]
|
|
])
|
|
]
|
|
];
|
|
parent::__construct($config);
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<h1 id="type-registry">Type Registry</h1>
|
|
<p>Every type must be presented in Schema with single instance (<strong>graphql-php</strong>
|
|
throws when it discovers several instances with the same <code>name</code> in schema).</p>
|
|
<p>Therefore if you define your type as separate PHP class you must ensure that only one
|
|
instance of that class is added to schema.</p>
|
|
<p>Typical way to do this is to create registry of your types:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
|
|
class TypeRegistry
|
|
{
|
|
private $myAType;
|
|
private $myBType;
|
|
|
|
public function myAType()
|
|
{
|
|
return $this->myAType ?: ($this->myAType = new MyAType($this));
|
|
}
|
|
|
|
public function myBType()
|
|
{
|
|
return $this->myBType ?: ($this->myBType = new MyBType($this));
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<p>And use this registry in type definition:</p>
|
|
<pre><code class="php"><?php
|
|
namespace MyApp;
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
class MyAType extends ObjectType
|
|
{
|
|
public function __construct(TypeRegistry $types)
|
|
{
|
|
parent::__construct([
|
|
'fields' => [
|
|
'b' => $types->myBType()
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Obviously you can automate this registry as you wish to reduce boilerplate or even
|
|
introduce Dependency Injection Container if your types have other dependencies.</p>
|
|
<p>Alternatively all methods of registry could be static if you prefer - then there is no need
|
|
to pass it in constructor - instead just use use <code>TypeRegistry::myAType()</code> in your type definitions.</p>
|
|
|
|
</div>
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
<a href="object-types/" class="btn btn-neutral float-right" title="Object Types">Next <span class="icon icon-circle-arrow-right"></span></a>
|
|
|
|
|
|
<a href="../getting-started/" class="btn btn-neutral" title="Getting Started"><span class="icon icon-circle-arrow-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<!-- Copyright etc -->
|
|
|
|
</div>
|
|
|
|
Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|
</footer>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
<div class="rst-versions" role="note" style="cursor: pointer">
|
|
<span class="rst-current-version" data-toggle="rst-current-version">
|
|
|
|
|
|
<span><a href="../getting-started/" style="color: #fcfcfc;">« Previous</a></span>
|
|
|
|
|
|
<span style="margin-left: 15px"><a href="object-types/" style="color: #fcfcfc">Next »</a></span>
|
|
|
|
</span>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|