mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-26 23:06:04 +03:00
357 lines
10 KiB
HTML
Executable File
357 lines
10 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">
|
|
|
|
|
|
<link rel="shortcut icon" href="../img/favicon.ico">
|
|
<title>Introduction - graphql-php</title>
|
|
<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>
|
|
|
|
</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 class="toctree-l1">
|
|
|
|
<a class="" href="..">About</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../getting-started/">Getting Started</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../complementary-tools/">Complementary Tools</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<span class="caption-text">Type Definitions</span>
|
|
<ul class="subnav">
|
|
<li class=" current">
|
|
|
|
<a class="current" href="./">Introduction</a>
|
|
<ul class="subnav">
|
|
|
|
<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="">
|
|
|
|
<a class="" href="object-types/">Object Types</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="scalar-types/">Scalar Types</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="enum-types/">Enumeration Types</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="lists-and-nonnulls/">Lists and Non-Null</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="interfaces/">Interfaces</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="unions/">Unions</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="input-types/">Mutations and Input Types</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="directives/">Directives</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="schema/">Schema</a>
|
|
</li>
|
|
<li class="">
|
|
|
|
<a class="" href="type-language/">Using Type Language</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../executing-queries/">Executing Queries</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../data-fetching/">Fetching Data</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../error-handling/">Handling Errors</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../security/">Security</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../how-it-works/">How it works</a>
|
|
</li>
|
|
|
|
<li class="toctree-l1">
|
|
|
|
<a class="" href="../reference/">Class Reference</a>
|
|
</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 Definitions »</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="schema/">Schema</a>. </p>
|
|
<p>In graphql-php <strong>type</strong> is an instance of internal class from
|
|
<code>GraphQL\Type\Definition</code> namespace: <a href="object-types/"><code>ObjectType</code></a>,
|
|
<a href="interfaces/"><code>InterfaceType</code></a>, <a href="unions/"><code>UnionType</code></a>, <a href="input-types/"><code>InputObjectType</code></a>,
|
|
<a href="scalar-types/"><code>ScalarType</code></a>, <a href="enum-types/"><code>EnumType</code></a> (or one of subclasses).</p>
|
|
<p>But most of the types in your schema will be <a href="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:</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>Using <a href="http://graphql.org/learn/schema/#type-language">GraphQL Type language</a>:</p>
|
|
<pre><code class="graphql">schema {
|
|
query: Query
|
|
mutation: Mutation
|
|
}
|
|
|
|
type Query {
|
|
greetings(input: HelloInput!): String!
|
|
}
|
|
|
|
input HelloInput {
|
|
firstName: String!
|
|
lastName: String
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Read more about type language definitions in a <a href="type-language/">dedicated docs section</a>.</p>
|
|
<h1 id="type-registry">Type Registry</h1>
|
|
<p>Every type must be presented in Schema by a single instance (<strong>graphql-php</strong>
|
|
throws when it discovers several instances with the same <strong>name</strong> in the 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 the schema.</p>
|
|
<p>The typical way to do this is to create a 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 the registry could be static - then there is no need
|
|
to pass it in constructor - instead just use use <strong>TypeRegistry::myAType()</strong> 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="../complementary-tools/" class="btn btn-neutral" title="Complementary Tools"><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="../complementary-tools/" style="color: #fcfcfc;">« Previous</a></span>
|
|
|
|
|
|
<span style="margin-left: 15px"><a href="object-types/" style="color: #fcfcfc">Next »</a></span>
|
|
|
|
</span>
|
|
</div>
|
|
<script>var base_url = '..';</script>
|
|
<script src="../js/theme.js"></script>
|
|
<script src="../search/require.js"></script>
|
|
<script src="../search/search.js"></script>
|
|
|
|
</body>
|
|
</html>
|