Apollo-client: error ApolloError: Expected (a schema) to be a GraphQL schema

Created on 16 Jul 2020  ·  7Comments  ·  Source: apollographql/apollo-client

Intended outcome:

Actual outcome:

How to reproduce the issue:

Versions

After upgrading to Apollo Client 3, my SSR no longer connects via Apollo-Schema-Link. Getting the error.

error ApolloError: Expected { __validationErrors: undefined, extensions: undefined, astNode: undefined, extensionASTNodes: [], __allowedLegacyNames: [], _queryType: Query, _mutationType: Mutation, _subscriptionType: null, _directives: [@skip, @include, @deprecated], _typeMap: { Query: Query, Int: Int, Boolean: Boolean, PotentialSeries: PotentialSeries, ID: ID, String: String, Float: Float, User: User, Email: Email, Vote: Vote, Sub: Sub, Date: Date,  __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, AffiliateSalesInput: AffiliateSalesInput, Subscriptions: Subscriptions, TagInput: TagInput, ViewLog: ViewLog, ContentType: ContentType }, _possibleTypeMap: {}, _implementations: {} } to be a GraphQL schema.

Not sure what's changed here, but I'm using the proper imports

import { renderToStringWithData } from '@apollo/client/react/ssr'
import { SchemaLink } from '@apollo/client/link/schema'
import {
  ApolloClient,
  ApolloLink,
  InMemoryCache,
  ApolloProvider,
} from '@apollo/client'
          const schemaLink = new SchemaLink({
            schema,
            context: { user, userId: user ? user._id : undefined },
          })

          const client = new ApolloClient({
            ssrMode: true,
            link: schemaLink,
            cache,
          })

Not sure how the schema that I'm passing in is suddenly no longer a valid GraphQL Schema. The error only shows up in useQuery return errors. Not seeing an error anywhere else.

This same setup worked up until moving off of apollo-client rc's and moving to the new import { SchemaLink } from '@apollo/client/link/schema' import.

I don't have a reproduction because of how complex these things are, but seeing as how the same code worked with the release candidate, it seems like a bug to me.

Most helpful comment

I just found the reason why it was wrapped with {default: ...}:
const schema = require('./schema') is not equivalent to import schemafrom './schema'.
You need to do const schema = require('./schema').default

All 7 comments

I should mention that there is no other errors present and that the client hydrates correctly using the exact same schema, so it's clearly valid.

More information. @apollo/link-schema works but @apollo/client/link/schema does not.

Fix incoming: #6624

This should now be fixed in @apollo/[email protected]. Let us know if it isn't fixed, and we can reopen this issue.

I'm facing the same issue here, I can use the schema in the client perfectly fine but when I try to use it with SSR fails with this error:

Server Error
Error: Expected { default: { __validationErrors: undefined, extensions: undefined, astNode: [Object], extensionASTNodes: [], __allowedLegacyNames: [], _queryType: Query, _mutationType: Mutation, _subscriptionType: undefined, _directives: [Array], _typeMap: [Object], _possibleTypeMap: {}, _implementations: {} } } to be a GraphQL schema.

My code:

function createIsomorphLink() {
  if (typeof window === 'undefined') {
    const { SchemaLink } = require('@apollo/client/link/schema');
    const schema = require('../../graphql/schema');
    return new SchemaLink({ schema });
  } else {
    const { HttpLink } = require('@apollo/client/link/http');
    return new HttpLink({
      uri: '/api/graphql',
      credentials: 'same-origin',
    });
  }
}

schema:

Object [Module] {
  default: GraphQLSchema {
    __validationErrors: undefined,
    extensions: undefined,
    astNode: {
      kind: 'SchemaDefinition',
      directives: [],
      operationTypes: [Array],
      loc: [Object]
    },
    extensionASTNodes: [],
    __allowedLegacyNames: [],
    _queryType: Query,
    _mutationType: Mutation,
    _subscriptionType: undefined,
    _directives: [ @skip, @include, @deprecated ],
    _typeMap: [Object: null prototype] {
      Query: Query,
      User: User,
      ID: ID,
      String: String,
      Role: Role,
      Boolean: Boolean,
      Zone: Zone,
      DateTime: DateTime,
      Client: Client,
      IdType: IdType,
      ClientAddress: ClientAddress,
      Int: Int,
      ContactType: ContactType,
      ClientPhone: ClientPhone,
      Loan: Loan,
      LoanType: LoanType,
      Float: Float,
      LoanStatus: LoanStatus,
      Periodicity: Periodicity,
      Mutation: Mutation,
      CreateUserInput: CreateUserInput,
      SignInInput: SignInInput,
      SignInPayload: SignInPayload,
      ClientInput: ClientInput,
      ClientAddressInput: ClientAddressInput,
      ClientPhoneInput: ClientPhoneInput,
      LoanInput: LoanInput,
      Date: Date,
      ZoneInput: ZoneInput,
      __Schema: __Schema,
      __Type: __Type,
      __TypeKind: __TypeKind,
      __Field: __Field,
      __InputValue: __InputValue,
      __EnumValue: __EnumValue,
      __Directive: __Directive,
      __DirectiveLocation: __DirectiveLocation,
      Time: Time,
      PaymentStatus: PaymentStatus,
      PaymentType: PaymentType
    },
    _possibleTypeMap: [Object: null prototype] {},
    _implementations: [Object: null prototype] {}
  }
}

dependencies:

"@apollo/client": "^3.1.4",
"apollo-link-schema": "^1.2.5",

Hi @nicosampler, I have exactly the same issue: using SchemaLink the schema gets wrapped into a {default: schema} object which don't pass the schema validation.
I need SchemaLink for SSR at build time (next.js).
Did you find a workaround?

I just found the reason why it was wrapped with {default: ...}:
const schema = require('./schema') is not equivalent to import schemafrom './schema'.
You need to do const schema = require('./schema').default

Was this page helpful?
0 / 5 - 0 ratings