Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl seems to be not aligned with the section Errors and Non-Nullability of the spec #1988

Closed
shafreenAnfar opened this issue Sep 30, 2021 · 0 comments · Fixed by ballerina-platform/module-ballerina-graphql#446
Assignees
Labels
module/graphql Issues related to Ballerina GraphQL module Points/4 Team/PCM Protocol connector packages related issues Type/Bug

Comments

@shafreenAnfar
Copy link
Contributor

shafreenAnfar commented Sep 30, 2021

Version: Beta2

Spec says the below,

"If an error is thrown while resolving a field, it should be treated as though the field returned null, and an error must be added to the "errors" list in the response."

but our implementation seems to be a bit different.

Consider the below apollo example, query and the expected behaviour.

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Book {
    title: String
    author: String
  }

  type Query {
    books: [Book]
  }
`;

const books = [
  {
    title: 'The Awakening',
    author: new Error("Could not finnd author"),
  },
  {
    title: 'City of Glass',
    author: 'Paul Auster',
  },
];

const resolvers = {
  Query: {
    books: () => books,
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});

Query

{
  books {
    title
    author
  }
}

Expected result

{
  "errors": [
    {
      "message": "Could not finnd author",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "books",
        0,
        "author"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Could not finnd author",
            "    at Object.<anonymous> (/Users/shafreen/Documents/pcm/graphql/appollo/graphql-server-example/index.js:17:13)",
            "    at Module._compile (node:internal/modules/cjs/loader:1101:14)",
            "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)",
            "    at Module.load (node:internal/modules/cjs/loader:981:32)",
            "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)",
            "    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)",
            "    at node:internal/main/run_main_module:17:47"
          ]
        }
      }
    }
  ],
  "data": {
    "books": [
      {
        "title": "The Awakening",
        "author": null
      },
      {
        "title": "City of Glass",
        "author": "Paul Auster"
      }
    ]
  }
}

Note: "author": null

Now consider the same program in Ballerina,

import ballerina/graphql;

service / on new graphql:Listener(4000) {

    resource function get books() returns Book[]? {

        return [new Book("The Awakening", error("Could not finnd author")), new Book("City of Glass", "Paul Auster")];
    }
}

service class Book {

    private string title;
    private string|error author;

    public function init(string title, string|error author) {
        self.title = title;
        self.author = author;
    }

    resource function get title() returns string? {
        return self.title;
    }
    resource function get author() returns string|error? {
        return self.author;
    }
}

and the response

{
  "errors": [
    {
      "message": "Could not finnd author",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "books",
        0,
        "author"
      ]
    }
  ],
  "data": {
    "books": [
      {
        "title": "The Awakening"
      },
      {
        "title": "City of Glass",
        "author": "Paul Auster"
      }
    ]
  }
}

Note: missing author attribute, ideally it should be there with null value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/graphql Issues related to Ballerina GraphQL module Points/4 Team/PCM Protocol connector packages related issues Type/Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants