GraphQLBundle icon indicating copy to clipboard operation
GraphQLBundle copied to clipboard

Throw a warning when we are using a deprecated field

Open Kocal opened this issue 7 years ago • 9 comments

Hey,

I'm currently writing some docs on field deprecation (because I always forgot how to do it :rofl: ) and I just encountered a weird behavior.

When we mark a field a field as deprecated and we still use it in queries, it would be nice to see a warning, no?

Human:
    type: object
    config:
        description: 
        fields:
            faith:
                deprecationReason: "We've lost faith in Humanity"

Query:
    type: object
    config:
         fields:
             humans: { type: "[Human]!" }

running query { humans { faith } } should returns something like that:

{
  "data": {
    "humans": [...]
  },
  "extension": {
    "deprecationWarnings": [
      {
        "message": "The field is marked as deprecated",
        "field": "humans.faith",
        "reason": "We've lost faith in Humanity"
      }
    ]
  }
}

What do you think?

Kocal avatar Sep 05 '18 17:09 Kocal

Also, it's probably more related to GraphQL spec or to https://github.com/webonyx/graphql-php ... :disappointed:

Kocal avatar Sep 05 '18 17:09 Kocal

Hi @Kocal, It seems that GraphQL specs doesn't say nothing about this on query runtime. The only way to know if a field is deprecated is to use introspection query. Adding a custom deprecationWarnings entry in extensions can always be done since the specs leave the possibility to add whatever we want.

mcg-web avatar Sep 06 '18 05:09 mcg-web

Do you know where I can start? I found how exceptions are handled and how to add a deprecationWarnings entry (I hope... :sweat_smile:), but I can't find where I can check depreciation of query fields :thinking: Maybe in the Executor or Resolver folder?

Kocal avatar Sep 06 '18 12:09 Kocal

This information should be present in the FieldDefinition. So using Schema extension with context and errorFormatter this could be done.

mcg-web avatar Sep 07 '18 13:09 mcg-web

I'm probably missing something, but how can I have access to the parsed incoming GraphQL query in my schema extension?

In fact, I'm not sure I should use a schema extension, maybe an event would be more appropriate, no?... :thinking:

Kocal avatar Sep 09 '18 06:09 Kocal

In your schema extension you should wrap all objects graphql resolver and use ResolveInfo to get the field query and if query fields are deprecated add them in a variable (an array on Dreprecated Schema Extension itself or using context?). I'm not sure if this is the best way but this can be done like that. A combination of different Events can maybe do the same thing in a cleaner way.

mcg-web avatar Sep 09 '18 07:09 mcg-web

#378 Should make this easier to implement

mcg-web avatar Sep 12 '18 06:09 mcg-web

Mmmh maybe, I will take a look because I'm stuck at the moment :rofl:

Kocal avatar Sep 12 '18 06:09 Kocal

Hey @mcg-web, I really want to continue my work on #377 because I really think it's a must have to show a warning when querying a deprecated field.

I need to know if your advices are still up-to-date with the new versions or not? Thanks :)

Kocal avatar Dec 18 '19 20:12 Kocal