webpack-graphql-server icon indicating copy to clipboard operation
webpack-graphql-server copied to clipboard

Example using two data sets related

Open somecallmemike opened this issue 8 years ago • 1 comments

I am brand new to graphql and I am having trouble deciphering how to link two related data sets together using this tool. I created a company-database.ts:

export const companies = [
  {
    id: "1",
    name: 'APM'
  },
  {
    id: "2",
    name: 'DSP'
  },
  {
    id: "3",
    name: 'APP'
  }
];

export const findCompany = (companies: Array<any>, id: string) => {
  return companies.find(company => company.id === id);
};

export const addCompany = (companies: Array<any>, company: any) => {
  company.push(company);
  return company;
};

As well as a company-type.ts that adds PersonType as a related node. I also added a companyId field to persons:

export const typeDef = `
type CompanyType {
    name: String
    id: String
    persons: [PersonType]
}
`;

export const resolver = {
  CompanyType: {
    matches(root, args, ctx) {
      return ctx.companies;
    }
  },
};
export const persons = [
  {
    id: "1",
    sex: 'male',
    name: 'miro',
    companyId: '1'
  },
  {
    id: "2",
    sex: 'female',
    name: 'lala',
    companyId: '2'    
  },
  {
    id: "3",
    sex: 'male',
    name: 'joe',
    companyId: '3'    
  }
];

And added everything to query.ts:

export const typeDef = `
# Root Query
type Query {
    testString: String
    testStringConnector: String
    rootMockedString: String
    mockedObject: MockedType
    someType: SomeType
    getPerson(id: String!): PersonType
    persons: [PersonType]
    getCompany(id: String!): CompanyType
    companies: [CompanyType]
}
`;

export const resolver = {
  Query: {
    getPerson(root, args, ctx) {
      return ctx.findPerson(ctx.persons, args.id);
    },
    persons(root, args, ctx) {
      return ctx.persons;
    },
    getCompany(root, args, ctx) {
      return ctx.findCompany(ctx.comanies, args.id);
    },
    companies(root, args, ctx) {
      return ctx.comanies;
    },
    testString() {
      return "it Works!";
    },
    testStringConnector(root, args, ctx) {
      return ctx.testConnector.testString;
    },
    someType(root, args, ctx) {
      return {testFloat: 303.0303, testInt: 666};
    },
  },
};

However the system is erroring with

CompanyType.matches defined in resolvers, but not in schema

Is there a special method for forming this relationship that I am not seeing?

somecallmemike avatar Aug 18 '17 16:08 somecallmemike

hey!

did you require the new company file? (https://github.com/DxCx/webpack-graphql-server/blob/master/src/schema/index.ts#L6)

DxCx avatar Aug 19 '17 06:08 DxCx