0

I am not sure what I am doing wrong. I cant query this that seems to be a very easy API using GraphQL?

https://data.brreg.no/enhetsregisteret/enhet.json

I have tried different solutions. But I get the same error/problem.

import { ApolloServer, gql, IResolverObject } from "apollo-server";
import fetch from "node-fetch";

const typeDefs = gql`
    type Company {
        organisasjonsnummer: Int
        navn: String
    }

    type Query {
        companies: [Company!]!
    }
`;

const resolvers: IResolverObject = {
    Query: {
        companies: async () => {
            const response = await fetch(
                "https://data.brreg.no/enhetsregisteret/enhet.json"
            );
            const data = await response.json();
            return data.results;
        }
    }
};

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

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

Query used in GraphQL Playground:

{
  companies{
    organisasjonsnummer
  }
}

Error received from GraphQL Playground:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.companies.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "companies"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Query.companies.",
            "    at completeValue (F:\\apollo-datasource-rest-example\\node_modules\\graphql\\execution\\execute.js:573:13)",
            "    at F:\\apollo-datasource-rest-example\\node_modules\\graphql\\execution\\execute.js:505:16",
            "    at process._tickCallback (internal/process/next_tick.js:68:7)"
          ]
        }
      }
    }
  ],
  "data": null
}
prashant
  • 2,808
  • 5
  • 26
  • 41

1 Answers1

1

Because the JSON return from that link does not have a field called "results" , so null is resolved for the companies root query but it is expected to have a non-nullable value because its schema is defined as [Company!]!.

You should return data.data from this resolver based on the JSON structure return from that link.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172