PHP code example of oligus / schema

1. Go to this page and download the library: Download oligus/schema library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oligus / schema example snippets


$schema = new Schema();
// Add directive
$directive = new DirectiveType('upper');
$directive->addLocation(ExecutableDirectiveLocation::FIELD());
$schema->addDirective($directive);

// Add interface
$interface = (new InterfaceType('Entity'))
    ->addField(new Field('id', new IDType(), new TypeModifier(false)))
    ->addField(new Field('name', new StringType()));
$schema->addInterface($interface);

// Add scalar
$scalar = new ScalarType('Url');
$schema->addScalar($scalar);

// Add object
$object = (new ObjectType('User'))
    ->addField(new Field('id', new IDType(), new TypeModifier(false)))
    ->addField(new Field('name', new StringType()))
    ->addField(new Field('age', new IntegerType()))
    ->addField(new Field('balance', new FloatType()))
    ->addField(new Field('isActive', new BooleanType()));

$object->addField(new Field('friends', $object, new TypeModifier(true, true, false)))
    ->addField(new Field('homepage', $scalar))
    ->implements($interface);

$schema->addObject($object);

// Add query object
$query =  (new ObjectType('Query'))
    ->addField(new Field('me', $object, new TypeModifier(true)));
$field = (new Field('friends', $object, new TypeModifier(true, true, false)))
    ->addArgument(new Argument('limit', new IntegerType(), new TypeModifier(), new ValueInteger(10)));
$query->addField($field);

$schema->addObject($query);

// Add input object
$input = (new InputType('ListUsersInput'))
    ->addField(new Field('limit', new IntegerType()))
    ->addField(new Field('since_id', new IDType()));

$schema->addInput($input);

// Add mutation object
$mutation =  new ObjectType('Mutation');
$field = (new Field('users', $object, new TypeModifier(true, true, false)))
    ->addArgument(new Argument('params', $input));
$mutation->addField($field);
$schema->addObject($mutation);

// Add union
$union = (new UnionType('MyUnion'))
    ->addObjectType(new ObjectType('Dog'))
    ->addObjectType(new ObjectType('Cat'))
    ->addObjectType(new ObjectType('Bird'));

$schema->addUnion($union);

// Set root types
$schema->setQuery($query);
$schema->setMutation($mutation);

$serializer = new SchemaSerializer();
$serializer->serialize($schema);

ScalarType

BooleanType
FloatType
IDType
IntegerType
StringType

InterfaceType

$typeModifier = new TypeModifier($nullable = false, $listable = true, $nullableList = false);
$type = new BooleanType($typeModifier);

$scalar = new ScalarType('Url', 'Url description');

$type = new BooleanType();

$object = new ObjectType('Wine');
$object->addField(new Field('name', new StringType()));
$object->addField(new Field('age', new IntegerType()));
$object->addField(new Field('size', new IntegerType()));

$interface = new InterfaceType('Wine');
$interface->addField(new Field('name', new StringType()));
$object->implements($interface);

$interface = new InterfaceType('Wine');
$interface->addField(new Field('name', new StringType()));
$interface->addField(new Field('age', new IntegerType()));
$interface->addField(new Field('size', new IntegerType()));

$union = new UnionType('Animals');
$union->addObjectType(new ObjectType('Dog'));
$union->addObjectType(new ObjectType('Cat'));

$enum = new EnumType('Direction', 'Different directions', ['SOUTH', 'NORTH']);
$enum->addEnum('EAST');
$enum->addEnum('WEST');

$directive = new DirectiveType('example', 'Example directive');
$directive->addLocation(ExecutableDirectiveLocation::FIELD());
$directive->addLocation(ExecutableDirectiveLocation::INLINE_FRAGMENT());

$object = new InputType('Animal');
$object->addField(new Field('name', new StringType()));
$object->addField(new Field('age', new IntegerType()));
$object->addField(new Field('weight', new IntegerType()));

$field = new Field('simpleField', new IntegerType());

$field = new Field('simpleField', new IntegerType(), new TypeModifier($nullable = false));

$field = new Field('booleanListArgField', new BooleanType(), new TypeModifier(true, true));

$argument = new Argument('booleanListArg', new BooleanType(), new TypeModifier(true, true, false));
$field->addArgument($argument);

$argument = new Argument('booleanArg', new BooleanType());

$argument = new Argument('intArg', new IntegerType(), new TypeModifier(false));
// intArg: Int! = 0

$argument = new Argument('intArg', new IntegerType(), null, new ValueInteger(0));
// intArg: Int = 0

$bool = new ValueBoolean(true);
$bool->getValue(); // true
echo $bool; // 'true'