fullstack-tutorial icon indicating copy to clipboard operation
fullstack-tutorial copied to clipboard

Getting error when first starting server in the tutorial

Open robert-barcelona opened this issue 6 years ago • 6 comments

I'm sure it's something I'm doing wrong, but I've just started the tutorial and when I first try to start the server I get

type Launch { ^^^^^^

SyntaxError: Unexpected identifier at new Script (vm.js:74:7) at createScript (vm.js:246:10) at Object.runInThisContext (vm.js:298:10) at Module._compile (internal/modules/cjs/loader.js:657:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:20:18) [nodemon] app crashed - waiting for file changes before starting...

My schema.js file is

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

const typeDefs = gql``

module.exports = typeDefs;


type Launch {
  id: ID!
    site: String
  mission: Mission
  rocket: Rocket
  isBooked: Boolean!
}

type Rocket {
  id: ID!
    name: String
  type: String
}

type User {
  id: ID!
    email: String!
    trips: [Launch]!
}

type Mission {
  name: String
  missionPatch(size: PatchSize): String
}

enum PatchSize {
  SMALL
  LARGE
}

type Mutation {
  # if false, booking trips failed -- check errors
  bookTrips(launchIds: [ID]!): TripUpdateResponse!

    # if false, cancellation failed -- check errors
  cancelTrip(launchId: ID!): TripUpdateResponse!

    login(email: String): String # login token
}

type Query {
  launches: [Launch]!
    launch(id: ID!): Launch
  # Queries for the current user
  me: User
}

and index.js is


const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');

const server = new ApolloServer({ typeDefs });

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

Not sure where the error lies as I was simply copying code from the tutorial....thanks for any clues.

robert-barcelona avatar Feb 19 '19 20:02 robert-barcelona

All your graphql code (type, enum, etc) needs to be within the gql template string where typeDefs is defined. I ran into this as well going through the tutorial since it doesn't mention it.

zachdixon avatar Feb 27 '19 23:02 zachdixon

@zachdixon Thanks Zach, came here with the same issue, while it's blatantly obvious now that it is mentioned, and I feel like a bit of an idiot for missing it, for beginners following a tutorial it really should be clear.

mforcer avatar Mar 02 '19 12:03 mforcer

Hello

Thanks for your tutorial but i have almost same error during following the tutorial I took place all of the steps of tutorial in the start/server stage. My code is =>

index.js const { ApolloServer } = require('apollo-server'); const typeDefs = require('./schema');

const server = new ApolloServer({typeDefs});

server.listen().then( ({ url }) => {

console.log(`Server ready at ${url}`);

});

schema.js const { gql } = require('apollo-server');

const typeDefs = gql`

type Query {
    launches: [Launch]!
    launch(id: ID!): Launch
    # Queries for the current user
    me: User
}

type Mutation {
    # if false, booking trips failed -- check errors
    bookTrips(launchIds: [ID]!): TripUpdateResponse!
  
    # if false, cancellation failed -- check errors
    cancelTrip(launchId: ID!): TripUpdateResponse!
  
    login(email: String): String # login token
}

type TripUpdateResponse {
    success: Boolean!
    message: String
    launches: [Launch]
}

type Launch {
    id: ID!
    site: String
    mission: Mission
    rocket: Rocket
    isBooked: Boolean!
}

type Rocket {
    id: ID!
    name: String
    type: String
}

type User {
    id: ID!
    email: String!
    trips: [Launch]!
}

type Mission {
    name: String
    missionPatch(size: PatchSize): String
}

enum PatchSize {
    SMALL
    LARGE
}   

`; module.exports = typeDefs;

and i ran the command "npm start" But i took the error below. G:\Study\GraphQL\apollo\fullstack-tutorial\start\server\node_modules\graphql-tools\dist\generate\concatenateTypeDefs.js:22 throw new _1.SchemaError("typeDef array must contain only strings and functions, got " + type); ^ Error: typeDef array must contain only strings and functions, got object

Please fix it asap. I stopped here.

I will appreciate if you help me in advance.

JhonMik avatar Mar 26 '19 04:03 JhonMik

Hi there, ive been stucked with the same scenario for quite a while as well.

/home/foxelvoret/code/fullstack-tutorial/start/server/src/datasources/launch.js:13 async getAllLaunches() { ^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier

@mforcer can we overcome this using the babel compiler ?

jimenezfer avatar Apr 20 '19 04:04 jimenezfer

As per the documentation says (which I also missed the first time):

Our schema will be based on these features. In src/schema.js, import gql from Apollo Server and create a variable called typeDefs for your schema. Your schema will go inside the gql function (between the backticks in this portion: gql``).

Your final file schema.js file should look like this:

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

const typeDefs = gql`
  type Query {
    launches: [Launch]!
    launch(id: ID!): Launch
    # Queries for the current user
    me: User
  }

  type Mutation {
    # if false, booking trips failed -- check errors
    bookTrips(launchIds: [ID]!): TripUpdateResponse!

    # if false, cancellation failed -- check errors
    cancelTrip(launchId: ID!): TripUpdateResponse!

    login(email: String): String # login token
  }

  type TripUpdateResponse {
    success: Boolean!
    message: String
    launches: [Launch]
  }

  type Launch {
    id: ID!
    site: String
    mission: Mission
    rocket: Rocket
    isBooked: Boolean!
  }

  type Rocket {
    id: ID!
    name: String
    type: String
  }

  type User {
    id: ID!
    email: String!
    trips: [Launch]!
  }

  type Mission {
    name: String
    missionPatch(size: PatchSize): String
  }

  enum PatchSize {
    SMALL
    LARGE
  }  
`;

module.exports = typeDefs;

httpJunkie avatar Nov 06 '19 15:11 httpJunkie

That didn't work for me. this is my user.gql

` type Company { name: String catchPhrase: String bs: String }

type Geo { lat: String lng: String }

type Address { street: String suite: String city: String zipcode: String geo: Geo }

type User { id: Int name: String username: String email: String phone: String website: String company: Company address: Address } type Query { users: [User!]! } `

zagamendes avatar Jun 24 '22 03:06 zagamendes