使用角度NGRX存储效果按ID查询数据

时间:2018-11-29 06:17:39

标签: angular ngrx apollo-client ngrx-effects

我正在使用ngrx 6和angular 6,我的代码如下:

  1. effects.ts

    import * as fromProjectMemberActions from '../project-member/project-member.actions';
    import * as fromProjectMemberGraphQL from '../../members/members.graphql';
    
    @Effect({dispatch: false})
    loadProjectMember$ = this.actions$.pipe(
    ofType(fromProjectMemberActions.ProjectMemberActionTypes.GetProjectMembers),
    tap(
      action => {
        this.apollo.watchQuery({
          query: fromProjectMemberGraphQL.GET_ALL_PROJECT_MEMBERS,
          variables: { 
            projectID : // Project ID is needed
          }
        }).valueChanges.subscribe((response: any) => {
            console.log(response)
            this.store.dispatch(
              new fromProjectMemberActions.UpsertProjectMembers({
                projectMembers: response.data.findAllProjectMember
              })
            )
            this.store.dispatch(
                  new fromProjectMemberActions.DoneLoadingProjectMembers()
              )
        })
      }
    )
    );
    
  2. project-member.actions.ts

    export class GetProjectMembers implements Action {
      readonly type = ProjectMemberActionTypes.GetProjectMembers;
    }
    export class UpsertProjectMembers implements Action {
      readonly type = ProjectMemberActionTypes.UpsertProjectMembers;
        constructor(public payload: { projectMembers: ProjectMember[] }) {}
      }
    
  3. members.graphql.ts

    import gql from 'graphql-tag';
    
     export const GET_ALL_PROJECT_MEMBERS = gql`
     query getAllProjectMembers($projectID: Long!) {
        findAllProjectMembers(projectId: $projectID) {
          id
          memberId
          memberType
       }
     }
    `;
    
  4. 在graphql操场上可以看到给定的graphql API

    query{
      findAllProjectMembers(projectId: projectID) {
        id
        memberId
        memberType
      }
    }
    

    如果将上面的projectID替换为一个数字,例如:2,它将成功查询数据

  5. 要通过以下URL传递编号1上使用的项目ID的URL

    http://localhost/project-manager/view-project/2/members
    

我需要帮助来提取上面的项目ID,并在效果文件中使用它来查询成员列表

1 个答案:

答案 0 :(得分:3)

我会说。

//更新project-member.actions.ts

export class GetProjectMembers implements Action {
  readonly type = ProjectMemberActionTypes.GetProjectMembers;
  constructor(public payload: { projectId: number }) {}
}

//并更新效果

@Effect({dispatch: false})
loadProjectMember$ = this.actions$.pipe(
ofType(fromProjectMemberActions.ProjectMemberActionTypes.GetProjectMembers),
map((action: any) => action.payload),
tap(
  payload => {
    this.apollo.watchQuery({
      query: fromProjectMemberGraphQL.GET_ALL_PROJECT_MEMBERS,
      variables: { 
        projectID : payload.projectId
      }
    }).valueChanges.subscribe((response: any) => {
        console.log(response)
        this.store.dispatch(
          new fromProjectMemberActions.UpsertProjectMembers({
            projectMembers: response.data.findAllProjectMember
          })
        )
        this.store.dispatch(
              new fromProjectMemberActions.DoneLoadingProjectMembers()
          )
    })
  }
)
);

//可能在组件中,或者在您调度操作的地方传递projectId

subscribe to router {
  this.store.dispatch(new GetProjectMembers({ projectId })
}