question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using 3rd-party DTOs in GraphQL

See original GitHub issue

It seems like there’s no way to use object which are not defined as @ApiResource in GraphQL - they are simply invisible. This is not that big of a limitation, since one can define this in YAML like so:

resources:
    libphonenumber\PhoneNumber: ~

Serialization groups are also not a problem, since Symfony Serializer can read the config from YAML:

libphonenumber\PhoneNumber:
    attributes:
        countryCode: &std
            groups: ['GraphQLQuery']
        nationalNumber: *std
        extension: *std
        italianLeadingZero: *std
        numberOfLeadingZeros: *std
        rawInput: *std
        countryCodeSource: *std
        preferredDomesticCarrierCode: *std

The biggest issue is IDs - it seems like you cannot have objects without IDs as resources. This makes sense from resource perspective, but for simple value objects it’s just not possible to get any IDs. Additionally the method above does not seem to work with core objects (e.g. \DateTimeZone) 😦

I looked into docs, but they’re very sparse for GraphQL. I will happily improve them, but I cannot find a reliable way of making this working for objects without IDs (short of hacking \ApiPlatform\Core\GraphQl\Type\SchemaBuilder)?

This is similar to https://github.com/api-platform/core/issues/1870, but deals with really a different issue - code which you cannot change.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
soyukacommented, May 3, 2019

Closing as duplicate, thanks about the resolution @kiler129

1reaction
kiler129commented, Apr 28, 2019

@Siregacvek:

That’s simple, you just use the standard Symfony DIC functionality of services decoration.

    App\Serializer\BlankIriConverter:
        decorates: api_platform.iri_converter
        arguments: ['@App\Serializer\BlankIriConverter.inner']

and the IRI converter itself will simply use composition like the following:

<?php

class BlankIriConverter implements IriConverterInterface
{
    private $composed;

    public function __construct(IriConverterInterface $composed)
    {
        $this->composed = $composed;
    }

    public function getItemFromIri(string $iri, array $context = [])
    {
        if (\substr($iri, 0, 1) === '_') {
            throw new ItemNotFoundException(....); //Prevent requesting objects with blank identifiers
        }

        return $this->converter->getItemFromIri($iri, $context);
    }

    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
    {
        try {
            return $this->converter->getIriFromItem($item, $referenceType);
        } catch (\Throwable $t) {
            return '_:' . [your blank identifier logic];
        }
    }

    public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string {
        try {
            return $this->converter->getIriFromResourceClass($resourceClass, $referenceType);
        } catch (\Throwable $t) {
            return '_:' . $resourceClass;
        }
    }

    public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string {
        try {
            return $this->converter->getItemIriFromResourceClass($resourceClass, $identifiers, $referenceType);
        } catch (\Throwable $t) {
            return '_:' . [your blank identifier logic];
        }
    }

    public function getSubresourceIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string {
        try {
            return $this->converter->getSubresourceIriFromResourceClass($resourceClass, $identifiers, $referenceType);
        } catch (\Throwable $t) {
            return '_:' . [your blank identifier logic];
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL Schemas vs. RESTful DTOs | Khalil Stemmler
GraphQL schemas serve a similar purpose to RESTful DTOs. One of the main differences is tooling. In this post, I aim to strengthen...
Read more >
Journey of a GraphQL query | Lift-off II: Resolvers
When our server receives the HTTP request, it first extracts the string with the GraphQL query. It parses and transforms it into something...
Read more >
Make a Call to a GraphQL Service from a Java Application
Once we run the Maven build command, the plugin will generate both DTOs and utility classes required for calling our GraphQL service.
Read more >
An Advanced GraphQL with Quarkus - Piotr's TechBlog
We use the same schema and entity model as in my previous article about Spring Boot and GraphQL. Our application exposes GraphQL API...
Read more >
modeling DTO between client and external API - Stack Overflow
Our Application expose an GraphQL API using Nestjs-GraphQL. ... the physical card, we are using a RESTful API of some 3rd party vendor....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found