nicolaslopezj / meteor-apollo-accounts

Meteor accounts in GraphQL
MIT License
146 stars 37 forks source link

Syntax for customizing the User schema with non-primitives? #52

Closed acomito closed 7 years ago

acomito commented 7 years ago

I saw that we can customize the profile here:

//set options
const options = {
    CreateUserProfileInput: `
        name: Name!
      `
};

// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts(options);

Is there any way to use objects? For instance if I need name to be an object like:

profile: {
 name: {
   first: String,
   last: String
  }
}

I have a user schema but just not sure the where/when/how to let meteor-apollo-accounts know about it:


export const UserSchema = [`

type Email {
  address: String
  verified: Boolean
}

type Name {
  first: String
  last: String
}

type Profile {
  name: Name
}

type User {
  emails: [Email]
  _id: String
  profile: Profile
  roles: [String]
}

type Query {
    user: User,
  }
`];
acomito commented 7 years ago

Seems like I'll find the answer here:

https://github.com/janikvonrotz/meteor-apollo-accounts-example/blob/master/server/schema.js

Any other hints would be appreciated

nicolaslopezj commented 7 years ago

That is an old example, now just create the User schema

acomito commented 7 years ago

@nicolaslopezj I do have a type User schema but meteor-apollo-accounts doesn't seem to be aware of it. Where do the two connect? The User schema is registered (I can do my own mutations on it) but createAccount() is not aware of it, and so it throws an errror with everything other than the default schema (profile: { Name: String } }

import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import { typeDefs, resolvers } from '/imports/api/schema';
import {loadSchema, getSchema} from 'graphql-loader'
import {initAccounts} from 'meteor/nicolaslopezj:apollo-accounts'
import cors from 'cors';

//set options
// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts({});

// Load all your resolvers and type definitions into graphql-loader
loadSchema({typeDefs, resolvers});

// Gets all the resolvers and type definitions loaded in graphql-loader
const schema = makeExecutableSchema(getSchema());

//create server
createApolloServer({ schema }, {
  configServer: graphQLServer => graphQLServer.use(cors()),
});
nicolaslopezj commented 7 years ago

Can you show me the error?

acomito commented 7 years ago

Variable "$profile" got invalid value {"cell":"42341","name":{"first":"fsadf","last":"fsfda"}}. In field "cell": Unknown field.

In graphiql my createUser mutation shows this:

username: String
email: String
password: HashedPassword
plainPassword: String
profile: CreateUserProfileInput

So somewhere along the line my schema is not getting into the packages mutations. I'm guessing that I'm missing something in my imports/startup/server/grapQL-config.js or how I load my schemas in imports/api/schema.js?

example here: https://github.com/acomito/spacebug-graphql.git

nicolaslopezj commented 7 years ago

Are you defining the input CreateUserProfileInput?

acomito commented 7 years ago

I have no explicitly set it. Where do you do that? Here?

//set options
const options = {
    CreateUserProfileInput: `
        name: Name!
      `
};

// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts(options);
nicolaslopezj commented 7 years ago

Define it in your schema

input CreateUserProfileInput {
  name: String!
  otherField: String
}
acomito commented 7 years ago

ahh okay

acomito commented 7 years ago

That works now. What if I want to nest data I'm inserting?

type Name {
  first: String
  last: String
}

input CreateUserProfileInput {
  name: Name
  cell: String
}
nicolaslopezj commented 7 years ago

Just define that type in other file