Building a Schema with graphql-php
Define types and a schema with webonyx/graphql-php.
graphql-php: The Reference Implementation
webonyx/graphql-php is the de-facto PHP port of the GraphQL reference implementation. It gives you the type system, parser, validator and executor. You define your schema either programmatically (PHP objects) or from SDL (schema-first). In this lesson we build a schema by hand so you understand exactly what each piece does.
composer require webonyx/graphql-phpScalars and the Type Registry
Every GraphQL value bottoms out in a scalar: Int, Float, String, Boolean, ID. In graphql-php these live on the Type facade. Because object types reference each other (and themselves), a common pattern is a static TypeRegistry that memoizes each type so you build it only once.
<?php
use GraphQL\Type\Definition\Type;
// Built-in scalars, returned as singletons:
var_dump(Type::int()->name); // "Int"
var_dump(Type::string()->name); // "String"
var_dump(Type::id()->name); // "ID"
var_dump(Type::nonNull(Type::string())->toString()); // "String!"
All lessons in this course
- GraphQL vs REST
- Building a Schema with graphql-php
- Resolvers, Mutations and Subscriptions
- Performance: N+1 and DataLoader