What could be causing my Graphql query to return a null value?

I have been working on setting up a GraphQL application to access data from games.espn.com. However, I am facing an issue where my queries are returning null values. I suspect that there may be something missing in terms of return or resolve functions in the code. Despite spending several days reviewing the code, I cannot pinpoint why the desired value is not being returned.

Below is a snippet from my schema.js file:

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLSchema,
  GraphQLList,
  GraphQLNonNull,
  GraphQLBoolean,
  GraphQLFloat
} = require('graphql');
const axios = require('axios');
const request = require('request');

// Define various GraphQL object types for different data structures like Player, Slots, Team, etc.

...

// The BoxscoreType defines the structure of the box score data and includes information about teams, scoring period ID, and home team bonus.

const BoxscoreType = new GraphQLObjectType({
  name: 'Boxscore',
  fields: () => ({
    teams: {
      type: TeamsType,
    },
    scoringPeriodId: {
      type: GraphQLInt,
    },
    matchupPeriodId: {
      type: GraphQLInt,
    },
    homeTeamBonus: {
      type: GraphQLInt,
    }

  })
});

// More GraphQL object types defined here...

// The EspnQuery represents the query structure for retrieving ESPN box score data based on specific parameters.

const EspnQuery = new GraphQLObjectType({
    name: 'EspnQuery',
    fields: {
      getBoxscore: {
        type: BoxscoreDataType,
        args: {
          leagueId: {
            name: 'leagueId',
            type: new GraphQLNonNull(GraphQLInt)
          },
          seasonId: {
            name: 'seasonId',
            type: new GraphQLNonNull(GraphQLInt)
          },
          teamId: {
            name: 'teamId',
            type: new GraphQLNonNull(GraphQLInt)
          },
          scoringPeriodId: {
            name: 'scoringPeriodId',
            type: new GraphQLNonNull(GraphQLInt)
          },
        },
        resolve: (obj, {leagueId, seasonId, teamId, scoringPeriodId}) => {
            // Logic to fetch data from the ESPN API using Axios library
        }
      }
    },
  });

// Lastly, export the GraphQL schema with EspnQuery as the root query.

module.exports = new GraphQLSchema({
  query: EspnQuery
});

When executing the following query in Graphiql:

{
    getBoxscore(leagueId: 1150587, seasonId: 2017, teamId: 5, scoringPeriodId: 7) {
        boxscore{
        teams {
            list0{
          slots{
            player0{
              player{
                firstName
              }
            }
          }
        }
       }
      }
    }
}

The resulting output is:

{
  "data": {
    "getBoxscore": {
      "boxscore": {
        "teams": {
          "list0": null
        }
      }
    }
  }
}

Answer №1

Your schema needs to be adjusted to match the data structure properly. When dealing with arrays in GraphQL, it's essential to use GraphQLList instead of unnecessarily adding ListType.

For instance, if you observe the JSON returned from the endpoint, teams is an array and not an object. Although you've defined a TeamType that corresponds to the team data structure, it's crucial to specify to GraphQL that teams will consist of multiple TeamType objects as a list, rather than just one. This can be achieved by writing:

teams: { type: new GraphQLList(TeamsType) }

In GraphQL schema language, this would appear as [TeamsType]. While most fields in TeamsType are objects or scalars, the slots field is also an array. To address this, you would define it similarly as:

slots: { type: new GraphQLList(SlotsType) }

And so forth.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Iterating through data to showcase vertical columns for a specific time span of 3 years, highlighting only the months with available data

If data has been available for every month over the past 3 years, I need it to be displayed in a specific format. You can refer to this example fiddle for reference: https://jsfiddle.net/bthorn/ncqn0jwy/ However, there are times when certain months may no ...

The mychannel channel encountered a discovery error: access denied. The evaluation of the transaction failed due to the following error: DiscoveryService reported an access denied error for my

Currently utilizing Hyperledger fabric 2.2 on an Ubuntu system, my goal is to set up a REST API using Express.js with guidance from this informative article. Both my fabric network and apiserver.js exist on the same server. Am I able to utilize localhost ...

Obtaining the Value of Input Text

Let's say you have the following HTML code: <form> Enter hash here: <input type="text" name="hash"> <button type="submit" formaction="/tasks/">Retrieve Url</button> </form> Is there a way ...

Adapting the column width to display or hide content with CSS styling

I have a row with 2 columns. The left column contains my main content and the right column is a chatroom. I would like users to be able to minimize and open the chatroom, which I already know how to do. However, when the chatroom is open, I want the left ...

Utilizing Firebase authentication to sign in via phone number in conjunction with Express on Node.js

Encountering an issue while implementing backend Firebase Authorization. After thoroughly reading the documentation (https://firebase.google.com/docs/auth/web/phone-auth), I came to understand that Authorization requires two steps: send phone send code ...

Using static files in node.js for a Pah reference

Hey there! I'm having trouble accessing a file and keep getting a module not found error. I've tried using both the root and public folder, but no luck so far. Could someone please help me with this? app.use(express.static('public')) v ...

What is the top choice instead of using the jQuery toggle() method?

After jQuery deprecated the toggle() method, I began searching for alternative ways to toggle classes on Stack Overflow. I came across various other methods to accomplish the same task (Alternative to jQuery's .toggle() method that supports eventData? ...

serving index.html using express and react

I am currently working on an application with Express, which includes a create-react-app. My goal is to display the index.html file located in the public folder of the create-react-app when a user visits the root URL. Below is the code snippet from my inde ...

Exploring portfinder in Javascript: A guide to its usage

As a newcomer to Javascript, I am eager to figure out how to utilize the portfinder.getPort() function within one of my functions in order to generate a random port each time. The code snippet below showcases my current implementation: var portfinder = re ...

Understanding how to translate the content of email confirmation and password reset pages in Parse Server

Is there a way to translate the Email Confirmation and Password Reset pages in my project using Parse server? I have searched everywhere for a solution but haven't found one yet. While I did come across information about email templates, I couldn&apos ...

Methods for preventing a component from remounting when an update to the URI is necessary (such as for filter buttons)

With the use of react-router v3 and redux, my route setup looks something like this: <Route name="app" path="/" component={App}> <IndexRoute component={Catalogue} /> <Route name="brand" path="/brand/(:brand)" component={Catalogue} /> ...

I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click. count = 1; function myF ...

The Aurelia application named "Greetings Earth" is experiencing complete malfunction

As I embark on creating a Sharepoint framework webpart, my aim is to incorporate Aurelia as the JavaScript framework. The process begins with generating a Sharepoint framework webpart using Yeoman, resulting in this folder structure. Here are the core fi ...

Encountering a CORS policy error post deployment of the front-end on Firebase and the back-end on Heroku

My friend and I are attempting to deploy a project that is fully functional on our local machines. However, after deploying it (frontend on Firebase and backend on Heroku), we started encountering CORS policy issues. app.use(cors()); var allowCrossD ...

Troubleshooting a TypeScript error when trying to access an imported service in Angular 2

I've been working on creating a form that allows users to input required details, which will trigger a server call and save the information. However, no matter what I try, I keep encountering the following error: ORIGINAL EXCEPTION: TypeError: this.po ...

Setting up the page header in Next.js

I integrated some themekit CSS into the head of my Next.js project, but I'm getting a warning in the browser console. What could be causing this issue? Expected server HTML to contain a matching <head> within a <div>. const Layout = ({ ch ...

The Ionic application encounters an issue with the $stateParams being

Can someone assist me with resolving an issue related to ionic $stateParams? Here is the state configuration: .state('tabs.categories', { url: "/categories/:parentID", views: { 'categories-tab': { templateU ...

What could be causing the console to display undefined?

Can anyone help me with an issue I'm having while making an AJAX call using fetch and promises? I have successfully displayed the temperatures, but for some reason, the location is showing up as undefined. Here is the code snippet: function getWeat ...

vuejs props become null upon page refresh

MyComponent.vue template: <ResponsiveNavigation :nav-links="navLinks" /> script data: () => ({ navLinks: [] }), created: function() { this.getSocialMediaLinks(); }, methods: { getSocialMediaLinks() { var self = this; ...

The error message "TypeError: botGuilds.find is not defined" indicates that the

I am working on an express website for my discord bot and I am encountering an issue where it is unable to catch the mutual guilds, resulting in the following error: TypeError: botGuilds.find is not a function at C:\Users\não te interessa&bs ...