Keystone: Initial account creation fails when project is started because of missing context

Created on 23 Jun 2020  路  7Comments  路  Source: keystonejs/keystone

Solution just to create context with:

const context = keystone.createContext({ skipAccessControl: true });

and add it to the exectueGraphQl statements in the initial-data.js file

needs-clarification

Most helpful comment

Thanks!
Just for clarity, this is what that could look like (works)
get the password from the console's log

const crypto = require('crypto');
const randomString = () => crypto.randomBytes(6).hexSlice();

module.exports = async keystone => {

  const context = keystone.createContext({ skipAccessControl: true });

  // Count existing users
  const {
    data: {
      _allUsersMeta: { count },
    },
  } = await keystone.executeGraphQL({ context, 
    query: `query {
      _allUsersMeta {
        count
      }
    }`,
  });

  if (count === 0) {
    const password = randomString();
    const email = '[email protected]';


    await keystone.executeGraphQL({ context,
      query: `mutation initialUser($password: String, $email: String) {
            createUser(data: {name: "Admin", email: $email, isAdmin: true, password: $password}) {
              id
            }
          }`,
      variables: { password, email },
    });

    console.log(`

User created:
  email: ${email}
  password: ${password}
Please change these details after initial login.
`);
  }
};

All 7 comments

Thanks!
Just for clarity, this is what that could look like (works)
get the password from the console's log

const crypto = require('crypto');
const randomString = () => crypto.randomBytes(6).hexSlice();

module.exports = async keystone => {

  const context = keystone.createContext({ skipAccessControl: true });

  // Count existing users
  const {
    data: {
      _allUsersMeta: { count },
    },
  } = await keystone.executeGraphQL({ context, 
    query: `query {
      _allUsersMeta {
        count
      }
    }`,
  });

  if (count === 0) {
    const password = randomString();
    const email = '[email protected]';


    await keystone.executeGraphQL({ context,
      query: `mutation initialUser($password: String, $email: String) {
            createUser(data: {name: "Admin", email: $email, isAdmin: true, password: $password}) {
              id
            }
          }`,
      variables: { password, email },
    });

    console.log(`

User created:
  email: ${email}
  password: ${password}
Please change these details after initial login.
`);
  }
};

Hi @Sylchi, could you please provide the steps to reproduce this issue. That will be awesome to debug this issue. Cheers!

Hi @Sylchi, could you please provide the steps to reproduce this issue. That will be awesome to debug this issue. Cheers!

Hi @singhArmani ! Its when you use:

yarn create keystone-app my-app

Select the starter module with users and auth. Since there is no initial user it will try to create one which fails because the graphql queries in the initial-data.js dont have context. So the end result is that initial user is not created.

Tested it just now and the issue still seems to persist. Very bad experience for newcomers and super easy fix. @myAlterX solution works just create context and add it to executegraphql functions as context parameter.

@myAlterX Thanks tried your code and it worked.

Same error, just after yarn create keystone-app my-app

Same and adding the context worked.

This problem was fixed in the latest version

Was this page helpful?
0 / 5 - 0 ratings

Related issues

molomby picture molomby  路  11Comments

thekevinbrown picture thekevinbrown  路  31Comments

JedWatson picture JedWatson  路  17Comments

cowjen01 picture cowjen01  路  13Comments

bholloway picture bholloway  路  18Comments